防止rails 3上的ruby解析JSON帖子 [英] Prevent ruby on rails 3 from parsing JSON post
问题描述
我在Ruby on Rails 3中有一个控制器方法,它接受application / JSON作为内容类型。这一切都按预期工作,但我实际上不希望rails自动解析POST请求正文中的JSON。此方法充当网关,只是将信息传递到队列中,并且可能非常大。我不想浪费时间将数据处理到@_params,因为这是不必要的。
I have a controller method in Ruby on Rails 3 that accepts application/JSON as the content type. This all works as expected, but I actually do not want rails to automatically parse the JSON in the body of the POST request. This method is acting as a gateway and just shuttles the information in to a queue and could be quite large. I do not want to waste the time processing the data in to the @_params since it's unnecessary.
我相信我可以通过设置内容类型来解决这个问题。请求的标头,但我想在HTTP请求的语义上正确。
I believe I could get around this by setting the content-type in the header of the request to something else, but I would like to be semantically correct for the HTTP requests.
如何禁用此功能?
编辑:
更具体地说,如何为这一条路线编辑此功能?
more specifically how can I edit this functionality for just this one route?
推荐答案
参数解析在actionpack的 lib / action_dispatch / middleware / params_parser.rb
中非常深入。
Parameter parsing is baked pretty deeply inside actionpack's lib/action_dispatch/middleware/params_parser.rb
.
I我说你最好的办法是用Rack拦截请求,就像这样。
I'd say the best you're going to get away with is intercepting the request with Rack, something like this.
在 lib / raw_json中。 rb
module Rack
class RawJSON
def initialize(app)
@app = app
end
def call(env)
request = Request.new(env)
if request.content_type =~ /application\/json/i
# test request.path here to limit your processing to particular actions
raw_json = env['rack.input'].read
env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
env['rack.input'] = StringIO.new("raw_json=#{raw_json}")
end
return @app.call(env)
end
end
end
在 config.ru
中,在调用 run< your app name> :: Application之前插入此内容
require 'raw_json'
use Rack::RawJSON
这篇关于防止rails 3上的ruby解析JSON帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!