How to parse JSON request body in Sinatra just once and expose it to all routes?

前端 未结 5 1951
暗喜
暗喜 2020-12-15 16:12

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\'         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 16:17

    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]

提交回复
热议问题