How to receive a JSON object with Rack

十年热恋 提交于 2019-11-30 15:07:07
env['rack.input'].gets

This worked for me. I found that using curl or wget to test POST requests against a Rack (v1.4.1) server required using this code as a fallback to get the request body. POST requests out in the wild (e.g. GitHub WebHooks) didn't have this same problem.

Eqbal

It seems that I'm supposed to use something like:

msg = JSON.parse env['rack.input'].read 

Then just use params in the msg hash.

At least it worked for me this way.

One more way to do that:

# ...
request  = Rack::Request.new env
object   = JSON.parse request.body.gets
# ...

See the documentation: www.rubydoc.info/gems/rack/Rack/Lint/InputWrapper

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