Skip to content

Instantly share code, notes, and snippets.

@beepony
Last active January 4, 2017 08:51
Show Gist options
  • Save beepony/0c5658930703fab938b446649c9eb9ef to your computer and use it in GitHub Desktop.
Save beepony/0c5658930703fab938b446649c9eb9ef to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby -w
# 使用 net/http 发送 post 请求,带上自定义的 header
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("http://xxx.com/foo/bar")
# 设置请求头
headers = {
:Authorization => "UPYUN bin:#{signature(params)}",
:Date => "#{gmdate}",
:User_Agent => "beepony"
}
# 设置 post 参数,post 把参数放到 request body 里发送
params = {
app_name: "compress",
bucket_name: "binimg",
notify_url: "http://xs.com/notify",
tasks: "#{tasks(zip)}"
}
# 初始化一个 http 对象
http = Net::HTTP.new(uri.host, uri.port)
# 使用 http 对象的 post 方法
response = http.post(uri.path, params.to_json, headers)
# 输出相应代码和响应体
puts response.code,response.body
response.each_header {|k,v| p "#{k}":"#{v}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment