2

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.

2
  • Why not use the Curb gem? It's a Ruby interface to curl's library. Commented Mar 14, 2013 at 7:21
  • Your channels value is not an array as it is in your curl request - may be that the service is expecting one and is throwing a really badly worded error as a result Commented Mar 15, 2013 at 3:53

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

2 Comments

I edited the comment. I add the port 443 so it could pass over SSL but, still having problem with invalid json
Your Ruby structure now isn't the same as the one in the curl example. Try putting square brackets around "Giants" e.g. ["Giants"], to make it an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.