Ruby on rails API请求不使用rest客户端,但使用法拉第/cURL
我有一个GET请求来查询JOOR API以检索他们的产品类别列表 此请求在cURL或Faraday gem上运行良好,但在rest客户机gem上运行不佳。测试请求结果如下: 1) 卷曲Ruby on rails API请求不使用rest客户端,但使用法拉第/cURL,ruby-on-rails,rest-client,faraday,Ruby On Rails,Rest Client,Faraday,我有一个GET请求来查询JOOR API以检索他们的产品类别列表 此请求在cURL或Faraday gem上运行良好,但在rest客户机gem上运行不佳。测试请求结果如下: 1) 卷曲 curl -X GET \ https://apisandbox.jooraccess.com/v2/categories/ \ -H 'Authorization: Bearer [mytoken]' \ -H 'Content-Type: application/json' 2) 与法拉第 co
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客户端
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)之间的区别是什么?为什么只有失败
==========更新==========
使用httplog gem记录法拉第和Rest客户端请求的头信息。这是日志
法拉第:
[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客户端处理之后,标头中的内容类型可能没有正确传入。因此,我使用rest客户端的方式可能不正确。如何更正它?尝试将其更改为您的rest客户端代码
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客户端代码
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客户端请求的头中添加
accept:'*/*'
。它可以工作。我必须在rest客户端请求的头中添加accept:'*/*'
。它是有效的。我看不出语法有任何错误,您有测试帐户的示例令牌吗try@Subash我能确认的是,所有电话都没有401问题。刚刚发现一些可能有用的东西,将在一分钟内更新问题。我看不出语法有任何错误,您有测试帐户的示例令牌吗try@Subash我能确认的是,所有电话都没有401问题。刚发现的内容可能会有所帮助,将在一分钟内更新问题。最好指定您接受的数据的格式,而不是使用/It指定您接受的数据的格式,而不是使用/It也可以接受任何内容。rest客户端似乎倾向于为我设置一些默认的accept值,这也很有效。rest客户端似乎倾向于为我设置一些默认的接受值。