我正在尝试使用.NET:HTTP gem将api-key添加到客户端的http头,但是当我尝试测试它时,它似乎似乎没有工作原因。基本上服务器需要客户端的http标头或任何东西都有http_x_api标头才能提供请求。
服务器代码
require 'sinatra'
before do
halt 400 if (env['API_KEY']) != 'wow'
end
get '/' do
"boo"
end
客户端代码
require 'net/http'
require 'uri'
port = ENV['PORT'] || '7474'
res = Net::HTTP.start('localhost', port ) { |h| h.get('/')}
res.add_field('api-key', 'wow')
res.each_header do |key, value|
p "#{key} => #{value}"
end
puts (res.code == '200' && res.body == 'boo') ? 'OK' : 'FAIL'
this the response i get back :=>
"x-frame-options => sameorigin"
"x-xss-protection => 1; mode=block"
"content-type => text/html;charset=utf-8"
"content-length => 0"
"connection => keep-alive"
"server => thin 1.5.0 codename Knife"
"api-key => wow"
FAIL
答案 0 :(得分:0)
在服务器上,env
中的HTTP标头变量以HTTP_
为前缀,因此您需要检查env['HTTP_API_KEY']
。来自the documentation:
HTTP_
变量:对应于客户端提供的HTTP请求标头的变量(即名称以HTTP_开头的变量)。这些变量的存在与否应与请求中是否存在适当的HTTP头相对应。