API请求无法与rest-client一起使用,但使用Faraday / cURL

时间:2018-06-08 05:41:59

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

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

此请求适用于cURL或法拉第宝石,但与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)得到了回复,投诉称“不支持请求/响应格式”

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

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

使用httplog gem记录法拉第和休息客户端请求的标头信息。这是日志。

法拉第:

[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的客户端:

[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>

它表明在rest-client处理后,标题中的Content-Type可能没有正确传递。所以我使用rest-client的方式可能不正确。我该如何纠正?

2 个答案:

答案 0 :(得分:2)

尝试将此更改为您的其他客户端代码

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

答案 1 :(得分:0)

我必须在标头中添加accept: '*/*'以获取rest-client请求。它有效。