【问题标题】:Ruby Rest-client keeps correcting the uriRuby Rest-client 不断纠正 uri
【发布时间】:2017-12-07 00:26:23
【问题描述】:

我正在使用 rest-client 向某个地址发出 post 请求。不幸的是,rest-client 不断将网络地址从“https”更正为“http”。然后返回一个错误。

我已经查看了文档,这是否可能是 gem 的设置。任何人都知道我如何“强制”rest-client 访问所需的地址。

提前谢谢!

使用的代码是:

  uri = 'https://sandbox.api.online.unit4.nl/V19/OAuth/Token'

  payload = {
    code: params[:code],
    client_id: '#{@client_id}',
    client_secret: '#{@client_secret}',
    redirect_uri: '#{@client_redirect_url}',
    grant_type: 'authorization_code'
  }

  response = RestClient::Request.execute(method: :post, url: uri,      headers: {params: payload})

卷曲:

> GET /V19/OAuth/Token HTTP/1.1
> Host: sandbox.api.online.unit4.nl
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 400 Bad Request
< content-length: 27
< content-type: application/json; charset=utf-8
< date: Wed, 06 Dec 2017 15:21:44 GMT
< p3p: CP="NON CUR OTPi OUR NOR UNI"
< server: Microsoft-IIS/7.5
< x-aspnetmvc-version: 5.2
< cache-control: no-cache, no-store, max-age=0, must-revalidate
< access-control-allow-origin: *
< x-powered-by: ASP.NET
< x-aspnet-version: 4.0.30319
< pragma: no-cache
< Set-Cookie: PD_STATEFUL_88eb7504-dfe9-11e2-8624-005056af4a32=sandbox.api.online.unit4.nl; Path=/
< Set-Cookie: LB_online_unit4=1493375404.20480.0000; path=/

【问题讨论】:

  • 你能把你正在使用的代码过去吗?另外,您确定没有重定向吗? curl -v YOUR_HTTPS_ADDRESS 返回什么?

标签: ruby-on-rails ruby rest-client


【解决方案1】:

可能是您的变量没有得到interpolated。使用单引号 '#{@variable}' 不起作用。尝试将单引号切换为双引号 ('' =&gt; "")。发生的情况是,您发送的不是实际的客户端 ID 和密码,而是#{@client_id}

试试

uri = 'https://sandbox.api.online.unit4.nl/V19/OAuth/Token'

  payload = {
    code: params[:code],
    client_id: @client_id,
    client_secret: @client_secret,
    redirect_uri: @client_redirect_url,
    grant_type: 'authorization_code'
  }

  response = RestClient::Request.execute(method: :post, url: uri, headers: {params: payload})

【讨论】:

  • 我试过了,但是值没有出现在查询字符串中,只有键出现了。所以我把双引号换成单引号解决了这个问题。不过可以再试一次,也许我忽略了一些东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 2016-09-29
  • 2020-03-22
  • 1970-01-01
相关资源
最近更新 更多