从Sinatra访问标头 [英] Accessing headers from Sinatra

查看:178
本文介绍了从Sinatra访问标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问sinatra中的过滤器中的标头。我的请求包括标题HTTP_AUTH,但我无法访问它。我的过滤器

I am trying to access the headers in a filter in sinatra. My request includes the header "HTTP_AUTH", however I can't access it. My filter is

before do
    halt 403 unless request['HTTP_AUTH'] == 'test'
end

它在我的机架测试中正常工作。

It works correctly from my rack test.

browser.get '/mypath', "CONTENT_TYPE" => "application/json", "HTTP_AUTH" => 'test'

但是当我从其他来源尝试时,我无法访问它。如果我 put request.env 我可以看到令牌在请求中,但我无法访问它。

But when I try from other sources I can't access it. If I puts request.env I can see the token is in the request, but I can't access it.

"HTTP_CONNECTION"=>"close", 
"HTTP_AUTH"=>"test", 
"HTTP_ACCEPT"=>"application/json", 

我做错了什么?

推荐答案

尝试使用之前使用阻止标题方法:

before do
  headers "HTTP_AUTH" => "test"
  headers "Content-Type" => "text/html; charset=utf-8"
end

或在请求中:

get '/' do
  headers['HTTP_AUTH'] = "test"
  headers['Cache-Control'] = 'public, max-age=600'
  puts headers # show headers on this request
end

使用 标题 只是哈希

这篇关于从Sinatra访问标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆