I have an GET request to query JOOR API to retrieve their product category list.
This request works fine with cURL or Faraday gem, but not working well with rest-client gem. The testing request results are below:
1) With cURL
curl -X GET \
https://apisandbox.jooraccess.com/v2/categories/ \
-H 'Authorization: Bearer [mytoken]' \
-H 'Content-Type: application/json'
2) With faraday
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) With rest-client
response = ::RestClient::Request.execute(method: :get, url: 'https://apisandbox.jooraccess.com/v2/categories', headers: {'Content-Type': 'application/json', 'Authorization': 'Oauth2 [mytoken]'})
1) and 2) both get expected response with a list of categories, but 3) got response with a complaint saying 'Request/Response format is not supported'
So what's the difference between 3) and 1)/2)? Why does only 3) fail?
========== UPDATE ==========
Used httplog gem to log the header info for both Faraday and Rest-client requests. Here's the log.
Faraday:
[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>
It shows that Content-Type in header probably didn't pass in correctly after processing by rest-client. So the way I use rest-client may be incorrect. How shall I correct it?
Try changing this to your rest-client code
response = ::RestClient::Request.execute(method: :get, url: 'https://apisandbox.jooraccess.com/v2/categories', headers: {'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Oauth2 [mytoken]'})