我在使用 rails 5 中的中间件时遇到问题,我试图POST
通过在中间件中使用来操纵在 http中发送Rack::Request
的参数,但是在调用函数之前,发送的参数不会被弹出@app.call(env)
。
使用 byebug 访问变量env
,request
结果如下:
(byebug) env.each{|key, value| p "#{key} --> #{value}"}
"rack.version --> [1, 3]"
"rack.errors --> #<IO:0x007fa00209eaa8>"
"rack.multithread --> true"
"rack.multiprocess --> false"
"rack.run_once --> false"
"SCRIPT_NAME --> "
"QUERY_STRING --> "
"SERVER_PROTOCOL --> HTTP/1.1"
"SERVER_SOFTWARE --> puma 3.6.2 Sleepy Sunday Serenity"
"GATEWAY_INTERFACE --> CGI/1.2"
"REQUEST_METHOD --> POST"
"REQUEST_PATH --> /some_path"
"REQUEST_URI --> /some_path"
"HTTP_VERSION --> HTTP/1.1"
"HTTP_CACHE_CONTROL --> no-cache"
"HTTP_POSTMAN_TOKEN --> 5a8514ac-9050-433e-be71-80316aa01e53"
"CONTENT_TYPE --> application/json"
"HTTP_AUTHORIZATION --> some_token"
"HTTP_USER_AGENT --> PostmanRuntime/3.0.9"
"HTTP_ACCEPT --> */*"
"HTTP_HOST --> localhost:3000"
"HTTP_COOKIE --> _session_id=session_id"
"HTTP_ACCEPT_ENCODING --> gzip, deflate"
"CONTENT_LENGTH --> 144"
"HTTP_CONNECTION --> keep-alive"
"SERVER_NAME --> localhost"
"SERVER_PORT --> 3000"
"PATH_INFO --> some_path"
"REMOTE_ADDR --> ::1"
"puma.socket --> #<TCPSocket:0x007fa008a1bc88>"
"rack.hijack? --> true"
"rack.hijack --> #<Puma::Client:0x007fa008a1bc60>"
"rack.input --> #<StringIO:0x007fa008a1a4c8>"
"rack.url_scheme --> http"
"rack.after_reply --> []"
"puma.config --> #<Puma::Configuration:0x007fa0021963e8>"
"action_dispatch.parameter_filter --> [:password]"
"action_dispatch.redirect_filter --> []"
"action_dispatch.secret_token --> "
"action_dispatch.secret_key_base --> some key"
"action_dispatch.show_exceptions --> true"
"action_dispatch.show_detailed_exceptions --> true"
"action_dispatch.logger --> #<ActiveSupport::Logger:0x007fa00256be78>"
"action_dispatch.backtrace_cleaner --> # <Rails::BacktraceCleaner:0x007fa00224e560>"
"action_dispatch.key_generator --> #<ActiveSupport::CachingKeyGenerator:0x007fa006231858>"
"action_dispatch.http_auth_salt --> http authentication"
"action_dispatch.signed_cookie_salt --> signed cookie"
"action_dispatch.encrypted_cookie_salt --> encrypted cookie"
"action_dispatch.encrypted_signed_cookie_salt --> signed encrypted cookie"
"action_dispatch.cookies_serializer --> json"
"action_dispatch.cookies_digest --> "
"action_dispatch.routes --> #<ActionDispatch::Routing::RouteSet:0x007fa002d59680>"
"ROUTES_70162609523520_SCRIPT_NAME --> "
"ORIGINAL_FULLPATH --> some_path"
"ORIGINAL_SCRIPT_NAME --> "
"action_dispatch.request_id --> e57576a4-5418-49f6-abaa-c49c5f7727a7"
"action_dispatch.remote_ip --> ::1"
"rack.session --> #<ActionDispatch::Request::Session:0x007fa008a314e8>"
"rack.session.options --> #<ActionDispatch::Request::Session::Options:0x007fa008a31498>"
正如您在 中看到的CONTENT_LENGTH
,该值144
消失了,但是当您执行命令request.params
时它是空的。
(byebug) request.params
{}
然后,如果我在执行该行时继续使用 byebug,@app.call(env)
则处理参数。
Processing by Api::SomeController#some_function as JSON
Parameters: {"some_params"=>{"param1"=>"1", "param2"=>"2", "param3"=>"3"}
这是中间件代码:
class SomeMiddleware
def initialize(*args)
@args = args
end
def call(env)
"#{self.class}::Logic".constantize.new(*@args).call(env)
end
class Logic
def initialize(app, additional=nil)
@app = app
@additional = additional
end
def call(env)
request = Rack::Request.new(env)
request.params
byebug
@app.call(env)
end
end
end