我正在尝试在 ruby 中发出 https 发布请求,它在 curl 中工作,但我在 ruby 中收到 400 个错误请求,我无法弄清楚原因。
这是红宝石代码:
require 'rubygems'
require 'net/https'
require 'uri'
require 'json'
uri = URI.parse("https://api.parse.com/1/push")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.host)
req['Content-Type'] = "application/json"
req['X-Parse-Application-Id'] = "abc123"
req['X-Parse-REST-API-Key'] = "abc123"
req.body = {:data => {:alert => "sup"}, :channels => ["notifications"]}.to_json
resp = http.start{|http| http.request(req)}
puts resp.inspect
这给了我一个#<Net::HTTPBadRequest 400 BAD_REQUEST readbody=true>
回应。
但是 curl 等效项工作得很好:
curl -X POST \
-H "X-Parse-Application-Id: abc123" \
-H "X-Parse-REST-API-Key: abc123" \
-H "Content-Type: application/json" \
-d '{
"channels": [
"notifications"
],
"data": {
"alert": "sup?"
}
}' \
https://api.parse.com/1/push
任何想法我做错了什么?