How to read POST data in rack request

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 16:38:00

问题


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

回答1:


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.




回答2:


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.




回答3:


You can try:

req.params

Hope this can help you.



来源:https://stackoverflow.com/questions/15458334/how-to-read-post-data-in-rack-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!