Sinatra 未通过重定向传递标头
我有一个简单的 Sinatra 代理,当调用端点时,它将重定向到同一 Sinatra 代理上的另一个端点。
当我使用标头发出请求时,当请求在第一个端点中重定向时,代理似乎不会将此标头传递到第二个端点。这是我的代码:
get '/first' do
# get the header from the request
username = env['HTTP_USERNAME']
# set the header for the response
response['username'] = username
redirect '/second'
end
get '/second' do
# This doesn't exist when redirected from /first
puts env['HTTP_USERNAME']
# Here is a list of all headers
env.each_key do |key|
puts "KEY: #{key} VALUE: #{env[key]}" unless key.nil?
end
"DONE"
end
任何提示将不胜感激。
谢谢
I have a simple Sinatra proxy, which when an endpoint is called, will redirect to another endpoint on the same Sinatra proxy.
When I make a request with a header, the proxy doesn't seem to pass this header through to the second endpoint when the request redirects in the first. This is my code:
get '/first' do
# get the header from the request
username = env['HTTP_USERNAME']
# set the header for the response
response['username'] = username
redirect '/second'
end
get '/second' do
# This doesn't exist when redirected from /first
puts env['HTTP_USERNAME']
# Here is a list of all headers
env.each_key do |key|
puts "KEY: #{key} VALUE: #{env[key]}" unless key.nil?
end
"DONE"
end
Any tips would be greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是故意的。
redirect
触发 HTTP 重定向,将触发新请求。此外,传递env
值是通过修改env
而不是response
来完成的。主要问题是,标题是什么意思?请求头还是响应头?从您的示例中,我认为您的意思是请求标头,因此
response['username'] = username
应该是request.env['username'] = username
。然后,您可以将redirect '/second'
替换为request.path_info = '/second'; pass
进行某种内部重定向。如果您不将该值传递给另一个 Rack 中间件/端点,您还可以将用户名存储在实例变量中。That is intentionally.
redirect
triggers an HTTP redirect, a new request will be fired. Also, passing onenv
values is done via modifyingenv
, notresponse
.The main question is, what do you mean by header? Request header or response header? From your example I figure you mean request header, therefore
response['username'] = username
should berequest.env['username'] = username
. You could then replaceredirect '/second'
withrequest.path_info = '/second'; pass
to do some sort of internal redirect. If you don't pass the value on to another Rack middleware/endpoint, you could also store the user name in an instance variable.