How to read POST data in rack request How to read POST data in rack request ruby ruby

How to read POST data in rack request


From reading the docs for POST, looks like it is giving you parsed data based on other content types. If you want to process "application/json", you probably need to

JSON.parse( req.body.read )

instead. To check this, try

puts req.body.read

where you currently have puts req.POST.


req.body is an I/O object, not a string. See the body documentation and view the source. You can see that this is in fact the same as mudasobwa's answer.

Note that other code in a Rack application may expect to read the same I/O, such as the param parsers in Sinatra or Rails. To ensure that they see the same data and not get an error, you may want to call req.body.rewind, possibly both before and after reading the request body in your code. However, if you are in such a situation, you might instead consider whether your framework has options to process JSON directly via some option on the controller or request content-type handler declaration etc - most likely there will be an option to handle this kind of request within the framework.


Try:

env['rack.input'].read

I found it in "How to receive a JSON object with Rack" and, though it still sounds weird to me, it likely works.


You can try:

req.params

Hope this can help you.