标头中的Rails RestClient传递令牌失败

时间:2014-08-05 11:20:49

标签: ruby-on-rails rest

我在终端做这个卷曲非常好:

$ curl https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000 -H "Authorization: Token xxxxxxxxxxxxxxxxxxxxxx"

输出是json。

现在我想通过我的rails应用程序访问json字符串。我已经尝试过RestClient来做这件事,但不知怎的,我总是得到401未经授权的错误。我相信令牌不能通过标头正确发送。我尝试过以下方法:

RestClient.get 'https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000', {token: 'xxxxxxxxxxxxxxxxxxxxxx'}

RestClient.get 'https://myurl.com/api/v1/orders/53e0ae7f6630361c46060000', :params => {:token => 'xxxxxxxxxxxxxxxxxxxxxx'}

没有成功。也许我使用错误的语法在标头中发送令牌? Doku在这里http://rubydoc.info/github/rest-client/rest-client - 我找不到任何错误。

1 个答案:

答案 0 :(得分:2)

# GET request with modified headers
RestClient.get 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}

# POST request with modified headers
RestClient.post 'http://example.com/resource', {:foo => 'bar', :baz => 'qux'}, {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}

# DELETE request with modified headers
RestClient.delete 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}

来源:https://github.com/rest-client/rest-client#headers