45

I am writing an API and it receives a JSON payload as the request body.

To get at it currently, I am doing something like this:

post '/doSomething' do
    request.body.rewind
    request_payload = JSON.parse request.body.read

    #do something with request_payload
    body request_payload['someKey']
end

What's a good way to abstract this away so that I don't need to do it for each route? Some of my routes are more complicated than this, and as a result the request.body would get reread and reparsed several times per route with this approach, which I want to avoid.

Is there some way to make the request_payload just magically available to routes? Like this:

post '/doSomething' do
    #do something with request_payload, it's already parsed and available
    body request_payload['someKey']
end
lmirosevic
  • 15,787
  • 13
  • 70
  • 116

5 Answers5

72

Use a sinatra before handler:

before do
  request.body.rewind
  @request_payload = JSON.parse request.body.read
end

this will expose it to the current request handler. If you want it exposed to all handlers, put it in a superclass and extend that class in your handlers.

mcfinnigan
  • 11,442
  • 35
  • 28
  • That was my first instinct, but will this work with async-sinatra? I'm afraid subsequent requests might override it while the previous ones are still in-flight? – lmirosevic Jun 11 '13 at 17:09
  • 1
    Sinatra *should* create a new instance of each handler per request, so provided you use an instance level variable it should be ok. We use a similar scheme and have seen no evidence of race conditions under load. – mcfinnigan Jun 11 '13 at 18:34
  • The `before` filter [can be](http://www.sinatrarb.com/intro.html#Filters) predicated on route patterns but seemingly not HTTP methods. Bummer - doing this for only POSTs is a plausible use case. – mgold Jun 30 '14 at 01:35
12

You can also use Rack Middleware to parse it. See https://github.com/rack/rack-contrib Just use Rack::PostBodyContentTypeParser when initializing your Sinatra class.

Harper Maddox
  • 540
  • 4
  • 8
  • 4
    Since `rack-contrib` 2.2.0, `PostBodyContentTypeParser` has been deprecated in favor of `JSONBodyParser`, much faster and modular. – Ulysse BN Apr 20 '20 at 14:46
6

Like this working for sinatra 1.4.5

before do
  if request.body.size > 0
    request.body.rewind
    @params = ActiveSupport::JSON.decode(request.body.read)
  end
end
Pavel Evstigneev
  • 4,918
  • 31
  • 21
3
before do
  request.body.rewind
  @request_payload = JSON.parse(request.body.read, symbolize_names: true)
end

So you can also symbolize_names while parsing JSON request body, this will give you access to your nested params like this @request_payload[:user]

Serj Petrenko
  • 51
  • 2
  • 5
3

You can parse your JSON post body as a Hash with Rack::PostBodyContentTypeParser from https://github.com/rack/rack-contrib:

require 'rack/contrib/post_body_content_type_parser'

class Api < Sinatra::Application
  use Rack::PostBodyContentTypeParser
  ...
end

You can even pass a custom block to Rack::PostBodyContentTypeParser to parse the JSON as symbols instead of strings:

a_proc = proc { |body| JSON.parse(body, symbolize_names: true, create_additions: false) }
use Rack::PostBodyContentTypeParser, &a_proc
Pere Joan Martorell
  • 2,608
  • 30
  • 29
  • 2
    I believe you answer is a duplicate of [this one](https://stackoverflow.com/a/34730743/6320039) with a few more details. Maybe you should edit it rather than creating a new answer? – Ulysse BN Apr 20 '20 at 14:55
  • @UlysseBN thanks for the suggestion, but I was getting a `Suggested edit queue is full` error, for this reason I decided to create a new answer – Pere Joan Martorell Apr 25 '22 at 14:19