Ada*_*amB 6 ruby curl net-http ruby-1.8.7
我正在尝试在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
Run Code Online (Sandbox Code Playgroud)
这给了我一个#<Net::HTTPBadRequest 400 BAD_REQUEST readbody=true>回应.
但卷曲等效工作正常:
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
Run Code Online (Sandbox Code Playgroud)
我有什么想法我做错了吗?
对我来说问题是没有通过SSL发出请求.一旦我设置了以下行,一切正常:
http.use_ssl = true
Run Code Online (Sandbox Code Playgroud)
这不是OP的问题,但我仍然发帖,因为错误信息是相同的,这个线程是错误的顶级谷歌点击之一.
您应该指定完整的网址:
req = Net::HTTP::Post.new("https://api.parse.com/1/push")
Run Code Online (Sandbox Code Playgroud)