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.read
really doing? I've Googled my fingers off and I just need a laymans answer on how POSTing JSON works.