Sinatra 未通过重定向传递标头

发布于 2024-09-04 11:24:26 字数 668 浏览 3 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-09-11 11:24:26

那是故意的。 redirect 触发 HTTP 重定向,将触发新请求。此外,传递 env 值是通过修改 env 而不是 response 来完成的。

主要问题是,标题是什么意思?请求头还是响应头?从您的示例中,我认为您的意思是请求标头,因此 response['username'] = username 应该是 request.env['username'] = username。然后,您可以将 redirect '/second' 替换为 request.path_info = '/second'; pass 进行某种内部重定向。如果您不将该值传递给另一个 Rack 中间件/端点,您还可以将用户名存储在实例变量中。

get '/first' do
  request.path_info = '/second'
  pass
end

get '/second' do
  puts request.env['HTTP_USERNAME']
  "DONE"
end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文