【问题标题】:rest-client gem: 401 unauthorized with tokenrest-client gem:401 未经授权的令牌
【发布时间】:2015-04-15 13:18:48
【问题描述】:

我正在使用rest-client gem 尝试向我的 API 发帖。

RestClient.post 帮助器需要三个参数来传递这样的标头:.post(url, params, headers)。您是否尝试过类似的方法?

RestClient.post('http://api.example.com/', {key: 'value'}, 授权: 'a2m...')

https://github.com/rest-client/rest-client/issues/339#issuecomment-71787018

我已遵循上述建议,但收到 RestClient::Unauthorized - 401 Unauthorized 作为回复。

我的代码:

RestClient.post "http://api.example-dev.com:7000/v1/resources", {key: 'value'}, :authorization => 'yyyyyyyy'

我用下面的 curl 命令成功了,但上面的 RestClient.post 没有。卷曲成功:

curl -i -X POST -d 'test[key]=1234' -H "Authorization: Token token=yyyyyyyyyyyyyy" \ http://api.example-dev.com:7000/v1/resources

【问题讨论】:

    标签: rest-client


    【解决方案1】:

    这应该产生与 curl 相同的请求:

    `RestClient.post "http://api.example-dev.com:7000/v1/resources", {:test => {key: '1234'}}, :authorization => 'Token token=yyyyyyyyyyyyyy'`
    

    【讨论】:

    • 谢谢。我认为rest-client在使用:authorization时会自动添加“Token token=”。我错了,您的回答为我解决了问题。
    猜你喜欢
    • 2016-05-17
    • 2014-01-30
    • 1970-01-01
    • 2011-11-16
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多