Parsing Params

Padrino is often used for web service applications. One common need these applications have is to parse incoming messages, typically JSON or XML. This data will come as part of the request's body instead of the typical form data approach, i.e. url parameters or multipart form data.

Here's when Rack::Parser comes in handy since it will do just that. In app/app.rb or in just add:

use Rack::Parser, :content_types => {
  'application/json'  => { |body| ::MultiJson.decode body }

Now all of your controllers will have the request's body JSON object parse as inside params and you would be able to do something along the lines of:

post '/people'
  order = params['name'] )
  # ...

Have a look at this great Sinatra recipe for a more detailed guide of how this middleware works.

last updated: 2022-02-22

comments powered by Disqus