【问题标题】:Ruby rest-client returning 401 while curl command worksRuby rest-client 在 curl 命令工作时返回 401
【发布时间】:2015-09-15 08:45:37
【问题描述】:

我有一个有效的 curl 命令,它返回我想要的,一堆 JSON:

curl -D- -u username:password -X GET -H "Content-Type: application/json" https://stash.address.net/rest/api/1.0/projects/FOOBAR/repos\?limit\=1000

我需要将其转换为 RestClient::Request,但我仍然得到 401:

RestClient::Request.execute(
    method: :get,
    headers: {
        content_type: 'application/json'
    },
    username: 'username',
    password: 'password',
    url: 'https://stash.address.net/rest/api/1.0/projects/FOOBAR/repos',
    params: {
        limit: '1000'
    },
    verify_ssl: false
)

我是不是忘记了什么?我的请求中是否缺少某些内容?是不是和上面的 curl 命令一模一样?

【问题讨论】:

  • 请不要使用verify_ssl: false,否则您实际上是在破坏 https 的全部目的。
  • 不使用会导致错误:“SSL_connect returned=1 errno=0 state=error: certificate verify failed (RestClient::SSLCertificateNotVerified)”这就是我添加它的原因。
  • 那么您还有另一个问题需要解决。您缺少一些证书或您拥有的证书已过时。不要走简单的路,要走正确的路。

标签: ruby curl get http-status-code-401 rest-client


【解决方案1】:

From the documentation 我没有看到任何提及usernameparams 选项。他们建议在 URL 中插入值。

RestClient.get 'https://username:password@stash.address.net/rest/api/1.0/projects/FOOBAR/repos', { accept: :json, params: { limit: 1000 }}

【讨论】:

  • 工作就像一个魅力!非常感谢,不胜感激!
  • username 后面的空格是故意的吗?
猜你喜欢
  • 2019-08-07
  • 2020-07-02
  • 2019-09-28
  • 1970-01-01
  • 2021-03-23
  • 2019-11-20
  • 2012-07-28
  • 1970-01-01
  • 2014-05-18
相关资源
最近更新 更多