ruby 如何在 Sinatra 中仅解析一次 JSON 请求正文并将其公开给所有路由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17049569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse JSON request body in Sinatra just once and expose it to all routes?
提问by lms
I am writing an API and it receives a JSON payload as the request body.
我正在编写一个 API,它接收一个 JSON 有效负载作为请求正文。
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.
什么是将其抽象出来以便我不需要为每条路线都这样做的好方法?我的一些路由比这更复杂,因此 request.body 会使用这种方法在每个路由中多次重新读取和重新解析,我想避免这种情况。
Is there some way to make the request_payload just magically available to routes? Like this:
有没有办法让 request_payload 神奇地可用于路由?像这样:
post '/doSomething' do
#do something with request_payload, it's already parsed and available
body request_payload['someKey']
end
回答by mcfinnigan
Use a sinatra before handler:
在处理程序之前使用 sinatra:
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.
这会将它暴露给当前的请求处理程序。如果您希望它向所有处理程序公开,请将其放在超类中并在您的处理程序中扩展该类。
回答by Harper Maddox
You can also use Rack Middleware to parse it. See https://github.com/rack/rack-contribJust use Rack::PostBodyContentTypeParserwhen initializing your Sinatra class.
您也可以使用机架中间件来解析它。请参阅https://github.com/rack/rack-contrib就use Rack::PostBodyContentTypeParser在初始化 Sinatra 类时。
回答by Pavel Evstigneev
Like this working for sinatra 1.4.5
像这样为 sinatra 1.4.5 工作
before do
if request.body.size > 0
request.body.rewind
@params = ActiveSupport::JSON.decode(request.body.read)
end
end
回答by Pere Joan Martorell
You can parse your JSON post body as a Hashwith Rack::PostBodyContentTypeParserfrom https://github.com/rack/rack-contrib:
您可以从https://github.com/rack/rack-contrib将您的 JSON 帖子正文解析为Hashwith :Rack::PostBodyContentTypeParser
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::PostBodyContentTypeParserto parse the JSON as symbols instead of strings:
您甚至可以传递一个自定义块来Rack::PostBodyContentTypeParser将 JSON 解析为符号而不是字符串:
a_proc = proc { |body| JSON.parse(body, symbolize_names: true, create_additions: false) }
use Rack::PostBodyContentTypeParser, &a_proc
回答by Serj Petrenko
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]
因此,您还可以在解析 JSON 请求正文时使用符号名称,这将使您可以像这样访问嵌套的参数 @request_payload[:user]

