30
votes

When I run the curl command

curl -v -H "Content-type: application/json" -X POST -d '{"name":"abc", "id":"12", "subject":"my subject"}' http://localhost:9292

to send a POST request with data to my Rack application, my code prints out {}. That is coming from puts req.POST() in the code below.

Why does it print out {} instead of the POST data? And how do I correctly access the POST data in my Rack application?

require 'json'

class Greeter
  def call(env)
    req = Rack::Request.new(env)
    if req.post?
      puts req.POST()
    end
    [200, {"Content-Type" => "application/json"}, [{x:"Hello World!"}.to_json]]
  end
end

run Greeter.new
3

3 Answers

29
votes

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.

14
votes

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.

2
votes

You can try:

req.params

Hope this can help you.