reading parameters on Sinatra post

后端 未结 1 1537
滥情空心
滥情空心 2021-01-21 17:32

I\'m working on my first Sinatra app and I have an hard time getting parameters from a post request.

I\'m using MiniTest::Spec and my spec looks like

 pa         


        
相关标签:
1条回答
  • 2021-01-21 17:47

    Sinatra just doesn't parse this data, because they are not form parameters.

    Form parameter would look like this

    curl -X POST 127.1:4567/ -d "foo=bar"

    Instead of params you can just use request.body.read or use rack contrib.

    rack-contrib

    1. Install it with gem install rack-contrib
    2. require it

    require 'rack'

    require 'rack/contrib'

    1. load it use Rack::PostBodyContentTypeParser

    with this you can use params as normal for json post data. Something like this:

    curl -X POST -H "Content-Type: application/json" -d '{"payload":"xyz"}' 127.1:4567/

    source for this: Sinatra controller params method coming in empty on JSON post request, http://jaywiggins.com/2010/03/using-rack-middleware-to-parse-json/

    0 讨论(0)
提交回复
热议问题