Search code examples
rubyjsonrestrest-client

Ruby Rest Client sending Escaped JSON


I'm using Sinatra and rest-client to send a JSON payload through and process it (the requests require keypair signatures).

However it looks like rest-client is sending escaped JSON through.

My RestClient log:

RestClient.post uriHere, "{\"eventID\":1}", "Accept"=>"application/json", "Accept-Encoding"=>"gzip, deflate", "Authorization"=>"Bearer tokenHere", "Content-Hash"=>"sha1 signatureHere", "Content-Length"=>"xyz", "Content-Type"=>"application/json", "Date"=>"Thu, 29 Jan 2015 22:26:47 GMT"

I can't verify really if the endpoint is reading the escaped characters, but if it's sending them like that, obviously it's not valid JSON. I've tried .gsub but it doesn't remove the extra slashes.

Thoughts?


Solution

  • I'm almost certain it's like that in the logs, i.e. the actual payload is valid JSON.

    Which means it is text, and should be passed through JSON.parse:

    new_hash = JSON.parse(the_payload)
    

    Then new_hash['eventID'] will be 1