Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read POST data in rack request

Tags:

ruby

rack

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 
like image 514
user782220 Avatar asked Mar 17 '13 06:03

user782220


People also ask

What is rack request?

Rack::Request provides a convenient interface to a Rack environment. It is stateless, the environment env passed to the constructor will be directly modified. req = Rack::Request. new(env) req.

What is ENV in rack?

Basically the env is a hash version of the request object specific to the web server. Rack does some work to give a normalized env so the middleware can behave consistently across web servers.

What is Rack middleware?

Rack middleware is a way to filter a request and response coming into your application. A middleware component sits between the client and the server, processing inbound requests and outbound responses, but it's more than interface that can be used to talk to web server.


2 Answers

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.

like image 81
Neil Slater Avatar answered Sep 21 '22 18:09

Neil Slater


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.

like image 35
Aleksei Matiushkin Avatar answered Sep 25 '22 18:09

Aleksei Matiushkin