[英]Why does using cURL to request from an API return 200/data but the equivalent Ruby/NetHTTP/HTTParty return 500/error?
I'm trying to get some data from the OneSignal API . 我正在尝试从OneSignal API中获取一些数据。
If I run it straight into the terminal (removed Auth key and App ID for this question), no problem: 如果我直接将其运行到终端中(此问题已删除了Auth密钥和App ID),则没有问题:
$ curl --include --header "Authorization: Basic Yzzzzzzzzzzzzzzzz"
"https://onesignal.com/api/v1/players?app_id={XXXXXXXXXXX}&limit=300&offset=0"
Returns: 返回值:
{"total_count":1,"offset":0,"limit":300,"players": [{"id":"XXXXXX","identifier":"XXXXXXX","session_count":3,"language":"en","timezone":-14400,"game_version":"3","device_os":"6.0.1","device_type":1,"device_model":"LG-K240","ad_id":"XXXXXXXX","tags":{},"last_active":1498938033,"playtime":0,"amount_spent":0.0,"created_at":1498935316,"invalid_identifier":false,"badge_count":0,"sdk":"030507","test_type":null}]}
If I use what I am lead to believe using this Curl to Ruby converter is the equivalent Ruby in my rake task, instead suddenly I hit unknown errors galore: 如果我使用的是导致我相信,在我的rake任务中使用此Curl to Ruby转换器就是等效的Ruby,相反,突然间,我遇到了很多未知错误:
require 'net/http'
require 'uri'
uri = URI.parse("https://onesignal.com/api/v1/players?app_id={XXXXXX}&limit=300&offset=0")
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Basic Yzzzzzzzzzzzzzzzz"
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts response.code
puts response.body
Returns: 返回值:
500
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>500 • OneSignal</title>
<style>
html, body {
margin: 0;
padding: 0;
background: #F1F1F1;
font-size: 18px;
}
svg#logo {
width: 240px;
height: 102px;
}
.logo-head {
margin-top: 1.3em;
}
svg#logo-head {
width: 40px;
height: 40px;
}
#content {
margin: 2em auto;
width: 600px;
padding: 30px;
text-align: center;
}
.text {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: rgba(0, 0, 0, 0.5);
font-size: 1em;
font-weight: 200;
line-height: 1.6em;
}
.text.twitter {
font-size: 0.85em;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:visited {
text-decoration: none;
color: inherit;
}
#message {
font-weight: 400;
font-size: 1.4em;
margin-bottom: 0;
}
#submessage {
font-size: 0.8em;
margin-bottom: 2.5em;
}
footer {
margin-top: 2em;
}
iframe {
border: 1px solid rgba(0, 0, 0, 0.1) !important;
border-radius: 4px;
}
</style>
</head>
<body>
<div id="content">
<svg version="1.1" id="logo" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px"
viewBox="0 0 160 34" style="enable-background:new 0 0 160 34;" xml:space="preserve">
<style type="text/css">
.st0{fill:#E44A49;}
.st1{fill:#515151;}
</style>
<g id="XMLID_1_">
<path id="XMLID_2_" class="st0" d="M55.3,27.1c-1.1,0-2.1-0.2-3-0.7c-0.9-0.4-1.7-1-2.3-1.8c-0.6-0.7-1.1-1.6-1.5-2.5
c-0.4-1-0.5-1.9-0.5-2.9s0.2-2,0.6-3s0.9-1.8,1.6-2.5s1.4-1.3,2.3-1.7s1.9-0.6,2.9-0.6c1.1,0,2.1,0.2,3,0.7s1.7,1.1,2.3,1.8
c0.6,0.7,1.1,1.6,1.5,2.5s0.5,1.9,0.5,2.9s-0.2,2-0.6,3s-0.9,1.8-1.5,2.5c-0.7,0.7-1.4,1.3-2.3,1.7C57.3,26.9,56.3,27.1,55.3,27.1z
M49.5,19.2c0,0.9,0.1,1.7,0.4,2.5s0.7,1.5,1.2,2.1s1.1,1.1,1.8,1.5c0.7,0.4,1.5,0.5,2.4,0.5s1.7-0.2,2.4-0.6
c0.7-0.4,1.3-0.9,1.8-1.5s0.9-1.3,1.2-2.1c0.3-0.8,0.4-1.6,0.4-2.4s-0.1-1.7-0.4-2.5s-0.7-1.5-1.2-2.1s-1.1-1.1-1.8-1.4
c-0.7-0.4-1.5-0.5-2.3-0.5c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="M154.3,10.9h2.9v12.2c0,0.9,0.4,1.3,1.2,1.3c0.2,0,0.3,0,0.6-0.1c0.2-0.1,0.4-0.1,0.6-0.2
l0.4,2.4c-0.4,0.2-0.9,0.3-1.4,0.4s-1,0.2-1.4,0.2c-0.9,0-1.6-0.2-2.1-0.7s-0.8-1.2-0.8-2.1V10.9z"/>
</g>
<path id="XMLID_8438_" class="st0" d="M18.5,0C8.3,0,0,8.3,0,18.5C0,25,3.4,30.7,8.4,34c0.3-0.2,0.6-0.4,0.9-0.6
c0.2-0.1,0.5-0.3,0.7-0.4c0.3-0.1,0.6-0.3,0.8-0.4c0.3-0.1,0.6-0.2,0.9-0.4c0.2-0.1,0.5-0.2,0.7-0.3c0.3-0.1,0.7-0.2,1-0.3
s0.5-0.1,0.8-0.2c0.3-0.1,0.5-0.1,0.8-0.2s0.6-0.1,0.9-0.1l0,0c0.3,0,0.7-0.1,1-0.1l0,0v-1.2v-1v-4.1v-1V20h-1v-2h1h3h1v5.4v1.1v4.2
v1v1.5l0,0c2.8,0.4,5.3,1.4,7.6,2.8l0,0c5.1-3.3,8.4-9,8.4-15.5C37,8.3,28.7,0,18.5,0z M24,18.5c0-3-2.5-5.5-5.5-5.5
S13,15.5,13,18.5c0,2.1,1.2,4,3,4.9v1.1c-2.3-1-4-3.3-4-6c0-3.6,2.9-6.5,6.5-6.5s6.5,2.9,6.5,6.5c0,2.3-1.2,4.3-3,5.5v-1.2
C23.2,21.7,24,20.2,24,18.5z M22,29.4v-1.1c4.1-1.4,7-5.3,7-9.9C29,12.7,24.3,8,18.5,8S8,12.7,8,18.5c0,4.9,3.4,9.1,8,10.2v1
c-5.1-1.1-9-5.7-9-11.2C7,12.1,12.1,7,18.5,7S30,12.1,30,18.5C30,23.6,26.6,28,22,29.4z"/>
</svg>
<p id="message" class="text">An Unexpected Error Occurred</p>
<p id="submessage" class="text">Error Code: 500</p>
<p id="explanation" class="text">
We track these errors automatically, but if the problem persists feel free to contact us.
</p>
</div>
</body>
</html>
This is insanity. 这是精神错乱。 Am I not making the EXACT same request?
我是否发出完全相同的请求? Why does the first one return the data I want (although I don't understand how to parse it into Ruby), but the second one hits an error?
为什么第一个返回我想要的数据(尽管我不知道如何将其解析为Ruby),但是第二个却出现错误?
Alternatively, how do I parse the successful response from using the curl
command into JSON or something my Ruby app can use? 另外,如何将使用
curl
命令转换为JSON或我的Ruby应用程序可以使用的内容来解析成功的响应?
string = `curl --include --header "Authorization: Basic Yzzzzzzzzzzzzzzzz" "https://onesignal.com/api/v1/players?app_id={XXXXXXX}&limit=300&offset=0"`
puts string
parsed = JSON.parse(string)
Returns: 返回值:
JSON::ParserError: 776: unexpected token at 'HTTP/1.1 200 OK
Date: Sat, 01 Jul 2017 20:06:17 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __yyyyyyyyyyyyyyy; expires=Sun, 01-Jul-18 20:06:17 GMT; path=/; domain=.onesignal.com; HttpOnly
Status: 200 OK
Cache-Control: max-age=0, private, must-revalidate
Access-Control-Allow-Origin: *
X-XSS-Protection: 1; mode=block
X-Request-Id: yyyyyyyyyyyy
Access-Control-Allow-Headers: SDK-Version
ETag: W/"e57da9b403c50294b432635d4ce4acfa"
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.065356
X-Content-Type-Options: nosniff
X-Powered-By: Phusion Passenger 5.1.4
Server: cloudflare-nginx
CF-RAY: 377bfc285ebe096f-SCL
{"total_count":1,"offset":0,"limit":300,"players":[{"id":"zzzzzzzzzz","identifier":"zzzzzzzzzz","session_count":3,"language":"en","timezone":-14400,"game_version":"3","device_os":"6.0.1","device_type":1,"device_model":"LG-K240","ad_id":"zzzzzzzzzzzzz","tags":{},"last_active":1498938033,"playtime":0,"amount_spent":0.0,"created_at":1498935316,"invalid_identifier":false,"badge_count":0,"sdk":"030507","test_type":null}]}'
/Users/Me/Code/my_project/lib/tasks/scheduler.rake:57:in `block in <top (required)>'
/Users/Me/.rvm/gems/ruby-2.2.3/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => daily_notifications
(See full trace by running task with --trace)
Edit: Curl response with --verbose 编辑:用--verbose卷曲响应
* Trying 104.16.204.165...
* TCP_NODELAY set
* Connected to onesignal.com (104.16.204.165) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: ssl473492.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
> GET /api/v1/players?app_id=zzzzzzzzzzzzzzzzzz&limit=300&offset=0 HTTP/1.1
> Host: onesignal.com
> User-Agent: curl/7.51.0
> Accept: */*
> Authorization: Basic Yzzzzzzzzzzzzzzzzzzz
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Thu, 06 Jul 2017 13:48:54 GMT
Date: Thu, 06 Jul 2017 13:48:54 GMT
< Content-Type: application/json; charset=utf-8
Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
Transfer-Encoding: chunked
< Connection: keep-alive
Connection: keep-alive
< Set-Cookie: __cfduid=d9b57682d26de2755426b575293a211d21499348933; expires=Fri, 06-Jul-18 13:48:53 GMT; path=/; domain=.onesignal.com; HttpOnly
Set-Cookie: __cfduid=d9b57682d26de2755426b575293a211d21499348933; expires=Fri, 06-Jul-18 13:48:53 GMT; path=/; domain=.onesignal.com; HttpOnly
< Status: 200 OK
Status: 200 OK
< Cache-Control: max-age=0, private, must-revalidate
Cache-Control: max-age=0, private, must-revalidate
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< X-Request-Id: d6276562-3cee-4c20-aa4e-5d781e7a7957
X-Request-Id: d6276562-3cee-4c20-aa4e-5d781e7a7957
< Access-Control-Allow-Headers: SDK-Version
Access-Control-Allow-Headers: SDK-Version
< ETag: W/"cef2128118655721c75aa04b81b2ce26"
ETag: W/"cef2128118655721c75aa04b81b2ce26"
< X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< X-Runtime: 0.019079
X-Runtime: 0.019079
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< X-Powered-By: Phusion Passenger 5.1.4
X-Powered-By: Phusion Passenger 5.1.4
< Server: cloudflare-nginx
Server: cloudflare-nginx
< CF-RAY: 37a306343f03162f-LIM
CF-RAY: 37a306343f03162f-LIM
<
* Curl_http_done: called premature == 0
* Connection #0 to host onesignal.com left intact
The only explanation is that the requests are different. 唯一的解释是请求是不同的。 First of all, I'd recommend using Faraday gem to help you with requests in Ruby.
首先,我建议使用Faraday gem来帮助您处理Ruby中的请求。 You can easily inspect Faraday object to see what went wrong.
您可以轻松检查法拉第物体,看看出了什么问题。 I can also see that you are passing
req_options
which is not part of your curl. 我还可以看到您正在传递
req_options
,这不是卷发的一部分。
So I advise you to make the simplest call with Faraday, perhaps even on a different endpoint to make sure it works and then with this endpoint. 因此,我建议您使用法拉第进行最简单的调用,甚至可以在其他端点上进行调用以确保其正常工作,然后再使用该端点进行调用。 If it's still not working, updating this question with Faraday request and response details would help further debug this
如果仍然无法使用,请使用法拉第请求和响应详细信息更新此问题,以帮助进一步调试
PS You should definitely not use Curl in your Ruby app. PS:您绝对不应在Ruby应用程序中使用Curl。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.