Sinatra not passing header with redirect
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
user开发者_如何学JAVAname = 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
That is intentionally. redirect
triggers an HTTP redirect, a new request will be fired. Also, passing on env
values is done via modifying env
, not response
.
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 be request.env['username'] = username
. You could then replace redirect '/second'
with request.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.
get '/first' do
request.path_info = '/second'
pass
end
get '/second' do
puts request.env['HTTP_USERNAME']
"DONE"
end
精彩评论