不知何故,HTTParty返回401,其中CURL正常工作。不知道如何在标题中传递令牌。
工作(200):
curl http://localhost:3020/api/products -H 'Authorization: Token token="111"'
不工作(401):
HTTParty.get('http://localhost:3020/api/products', headers: {"Authorization: Token token" => '111'})
我尝试过"Authorization" => '111'
和"token" => '111'
但结果相同。
答案 0 :(得分:29)
管理以使其工作如下。
HTTParty.get("http://localhost:3020/api/products", headers: {"Authorization" => "Token token=\"111\""})
答案 1 :(得分:3)
如果您想动态设置类的标题,这也有效,此示例用于获取Dun和Bradstreet的授权标记
require 'httparty'
require 'certified'
class DnbAuth
include HTTParty
debug_output $stdout
base_uri "https://maxcvservices.dnb.com/rest/Authentication"
def initialize(ct,u,p)
self.class.headers 'Content-type' => "#{ct}"
self.class.headers 'x-dnb-user' => "#{u}"
self.class.headers 'x-dnb-pwd'=> "#{p}"
end
def token()
response = self.class.post("/")
end
end
ct = 'text/xml'
u = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
p = 'xxxxxx'
xx = DnbAuth.new(ct,u,p)
puts xx.token.message