我在使用rails 5中的中间件时遇到问题,我试图通过在中间件中使用POST
来操纵Rack::Request
http中发送的参数,但发送的参数不会被弹出,直到调用函数@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
{}
然后,如果我在执行行@app.call(env)
时继续使用byebug,则会处理params。
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
答案 0 :(得分:2)
您可以尝试通过env建立请求
def call(env)
request = Rack::Request.new(env)
request.params
end
答案 1 :(得分:1)
这对我有用:
class MyClass
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
request.params['my-param']
end
end
答案 2 :(得分:0)
这应该让您查看正在发送的参数:
params = Rack::Utils.parse_query(env['rack.input'].read, "&")
答案 3 :(得分:0)
它对我有用
Comparator.comparingInt(String::length)
这样就找到了body上的params
params = JSON.parse(env['rack.input'].read.blank? ? {} : env['rack.input'].read)
使用 request = Rack::Request.new(env)
request.params
我在 URL 上找到了参数