API 请求不与 rest-client 一起工作但与 Faraday/cURL 一起工作

API request not working with rest-client but working with Faraday/cURL

我有一个 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) rest-client

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

1) 和 2) 都得到了带有类别列表的预期响应,但是 3) 得到了带有投诉 'Request/Response format is not supported'

的响应

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

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

使用 httplog gem 记录法拉第和 Rest-client 请求的 header 信息。这是日志。

法拉第:

[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": "",
...

Rest-Client:

[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 的方式可能不正确。我该如何纠正?

尝试将其更改为您的 rest-client 代码

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

我必须在 header 中为 rest-client 请求添加 accept: '*/*'。并且有效。