【问题标题】:API request not working with rest-client but working with Faraday/cURLAPI 请求不与 rest-client 一起使用,但与 Faraday/cURL 一起使用
【发布时间】:2018-06-08 05:41:59
【问题描述】:

我有一个 GET 请求来查询 JOOR API 以检索他们的产品类别列表。

此请求适用于 cURL 或 Faraday gem,但不适用于 rest-client gem。测试请求结果如下:

1) 使用 cURL

curl -X GET \
  https://apisandbox.jooraccess.com/v2/categories/ \
  -H 'Authorization: Bearer [mytoken]' \
  -H 'Content-Type: application/json'

2) 与法拉第

conn = Faraday.new(url: 'https://apisandbox.jooraccess.com/v2/categories')
conn.get do |req|
  req.headers['Content-Type'] = 'application/json'
  req.headers['Authorization'] = 'Oauth2 [mytoken]'
end

3) 使用休息客户端

response = ::RestClient::Request.execute(method: :get, url: 'https://apisandbox.jooraccess.com/v2/categories', headers: {'Content-Type': 'application/json', 'Authorization': 'Oauth2 [mytoken]'})

1) 和 2) 都获得了带有类别列表的预期响应,但 3) 收到了带有“不支持请求/响应格式”的投诉的响应

那么 3) 和 1)/2) 有什么区别?为什么只有 3) 失败?

========== 更新==========

使用 httplog gem 记录 Faraday 和 Rest-client 请求的标头信息。这是日志。

法拉第:

[httplog] Connecting: apisandbox.jooraccess.com:443
[httplog] Sending: GET http://apisandbox.jooraccess.com:443/v2/categories
[httplog] Header: user-agent: Faraday v0.9.2
[httplog] Header: content-type: application/json
[httplog] Header: authorization: Oauth2 [mytoken]
[httplog] Header: accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
[httplog] Header: accept: */*
[httplog] Header: connection: close
[httplog] Data:
[httplog] Status: 200
[httplog] Benchmark: 0.2766950000077486 seconds
[httplog] Header: content-type: application/json
[httplog] Header: date: Fri, 08 Jun 2018 05:56:16 GMT
[httplog] Header: server: nginx
[httplog] Header: server-time: 2018-06-08T05:56:16+00:00+00:00
[httplog] Header: vary: Cookie
[httplog] Header: content-length: 6958
[httplog] Header: connection: Close
[httplog] Response:
{
    "categories_list": {
        "response": {
            "code": "0",
            "comment": "",
...

休息客户端:

[httplog] Connecting: apisandbox.jooraccess.com:443
[httplog] Sending: GET http://apisandbox.jooraccess.com:443/v2/categories
[httplog] Header: accept: */*; q=0.5, application/xml
[httplog] Header: accept-encoding: gzip, deflate
[httplog] Header: content-type: application/json
[httplog] Header: authorization: Oauth2 [mytoken]
[httplog] Header: user-agent: Ruby
[httplog] Data:
[httplog] Status: 200
[httplog] Benchmark: 0.25124399999913294 seconds
[httplog] Header: content-type: application/xml
[httplog] Header: date: Fri, 08 Jun 2018 06:05:31 GMT
[httplog] Header: server: nginx
[httplog] Header: server-time: 2018-06-08T06:05:31+00:00+00:00
[httplog] Header: vary: Cookie
[httplog] Header: content-length: 202
[httplog] Header: connection: keep-alive
[httplog] Response:
<?xml version="1.0" encoding="UTF-8"?>
<response>
    <code>8</code>
    <comment>Request/Response format is not supported</comment>
    <log_id>9950b3b7-0e92-4514-9d2b-29d4b006fdc8</log_id>
</response>

这表明 header 中的 Content-Type 可能在 rest-client 处理后没有正确传递。所以我使用 rest-client 的方式可能不正确。怎么改?

【问题讨论】:

  • 我看不出语法有什么问题,你有我可以尝试的测试帐户的示例令牌
  • @Subash 我可以确认的是所有呼叫都没有 401 问题。刚刚发现一些可能有帮助的东西,将在一分钟内更新问题。

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


【解决方案1】:

尝试将其更改为您的休息客户端代码

response = ::RestClient::Request.execute(method: :get, url: 'https://apisandbox.jooraccess.com/v2/categories', headers: {'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Oauth2 [mytoken]'})

【讨论】:

  • 这也有效。似乎 rest-client 倾向于为我设置一些默认的接受值。
【解决方案2】:

我必须在休息客户端请求的标题中添加accept: '*/*'。它有效。

【讨论】:

  • 最好指定您接受的数据格式,而不是使用 / 接受任何内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
  • 2020-09-15
  • 2019-12-20
  • 2019-11-08
  • 2016-07-16
相关资源
最近更新 更多