1

I'm trying to understand the chain of events of how to POST a JSON object to a Sinatra API.

There will be a JSON object that will need to be passed through a POST request using Sinatra and here's my code that doesn't want to work:

namespace '/api/v1/registrations' do
    post '/' do
        my_hash = JSON.parse(request.body.read)
        puts my_hash
    end     
end

Here is some test data I'm trying to post:

curl -H "Content-Type: application/json" -X POST http://localhost:4567/api/v1/registrations/ -d '{"i": "am json"}'

I get this error on the console:

JSON::ParserError - 757: unexpected token at '{i: am':

It seems that I would be able to POST a JSON object to /api/v1/registrations/ and I would see it parsed on the console so I can see it actually did something.

What is request.body.readreally doing? I've Googled my fingers off and I just need a laymans answer on how POSTing JSON works.

Craigfoo
  • 313
  • 2
  • 12
  • Probably should mention I'm using Windows. – Craigfoo Feb 09 '15 at 00:44
  • This works ok for me on my Mac. I think your issue is due to how Windows escapes the quotes in your call to curl. You could try looking at escaping the `"` characters (sorry I don’t have a Windows machine so I don’t know how you’d do that). – matt Feb 09 '15 at 01:20
  • I think this is a windows thing, looking into other solutions like this one: http://stackoverflow.com/questions/11834238/curl-post-command-line-on-windows-restful-service – Craigfoo Feb 09 '15 at 03:20
  • 1
    You could use cygwin, or the git-bash terminal and avoid the "Windows problem" and see if you get the same problem. – ian Feb 09 '15 at 15:16

0 Answers0