Reputation: 38960
I have the following ruby code that I'm trying to implement in the same way that this curl
works.
curl -X POST \
-H "X-Parse-Application-Id: $APP_ID" \
-H "X-Parse-REST-API-Key: $KEY" \
-H "Content-Type: application/json" \
-d '{"score": 1337, "playerName": "Sean Plott", "cheatMode": false }' \
https://api.parse.com/1/classes/GameScore
Here's the ruby code:
def execute_post
puts "executing post script.."
payload ={
"score" => "1337",
"playerName" => "Sean Plott",
"cheatMode" => "false"
}.to_json
headers = {
'X-Parse-Application-Id' => $APP_ID,
'X-Parse-REST-API-KEY' => $KEY,
'Content-Type' => 'application/json'
}
url = "https://" + $DOMAIN + $BASE_URL + $LIST_CLASS
uri = URI.parse(url)
puts uri
puts payload
puts "***************"
req = Net::HTTP::Post.new(uri.host, initheader = headers)
req.body = payload
response = Net::HTTP.new(uri.host, uri.port).start{|http| http.request(req)}
puts "Response #{response.code} #{response.message}: #{response.body}"
end
For some reason I'm getting an error. Any ideas on what's going wrong?
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:135:in `sysread': end of file reached (EOFError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2024:in `read_status_line'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2013:in `read_new'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1050:in `request'
from parse_connect.rb:33:in `execute_post'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:543:in `start'
from parse_connect.rb:33:in `execute_post'
from parse_connect.rb:42
Upvotes: 3
Views: 3069
Reputation: 3519
In your curl example, you are using HTTPS.
You need to specifically enable HTTPS when using Net::HTTP (it's not enough to just put https:// in your URL).
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.start{|http| http.request(req)}
Upvotes: 0
Reputation: 55002
try it like this:
require 'mechanize'
Mechanize.new.post url, data.to_json, headers
if that doesn't work run fiddler and try it like this:
Mechanize.new{|a| a.set_proxy 'localhost', 8888}.post url, data.to_json, headers
Then inspect the request in fiddler
Upvotes: 1