Question

I'm having trouble converting a curl to a ruby script. Basically I'm trying to consume the parse.com RESTful api. Here is the curl

curl -X POST \
-H "X-Parse-Application-Id: XXX" \
-H "X-Parse-REST-API-Key: YYY" \
-H "Content-Type: application/json" \
-d '{
   "channels": [
     "Giants",
     "Mets"
   ],
   "data": {
     "alert": "test message"
   }
 }' \
 https://api.parse.com/1/push

And this Is what I've been trying to do (using HttParty)

puts "hello world"

require 'httparty'

require 'json'

class Parse
    include HTTParty
    base_uri "api.parse.com:443"
    headers "X-Parse-Application-Id" => "XXX", "X-Parse-REST-API-Key" =>  "YYY", "Content-Type" => "application/json"

end

option = {:channels => "Giants", :data => {:alert => "un mensaje de mierda"}}
puts Parse.post("/1/push", body: option.to_json)

)

I'm getting an error code 107 saying invalid json. Any suggestions will be appreciated.

Was it helpful?

Solution

I think you just need to serialise the ruby structure to JSON in the second param. The param should be the string to POST, not a Ruby struct.

I think this will work (only other possible problem I can see is whether you'll connect via https as the code is written):

data = {"channels": ['Giants'], "data": {alert: 'un mensaje '}}
puts Parse.post("/1/push", body: data.to_json)

. . . the JSON-like format in the Ruby data structure is not JSON,

foo: "bar"

is just another Ruby (1.9+) way of saying

:foo => "bar"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top