它是一个 RESTAPI,我需要向 onesignal 发送 get 请求。
网址=https://onesignal.com/api/v1/apps
他们提到的只是这样:
curl --include \
--header "Authorization: Basic your_key_here" \
https://onesignal.com/api/v1/apps
我尝试过这样的事情:
def index
url = URI.parse('https://onesignal.com/api/v1/apps?apikey=mykey')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body
end
但这不起作用。 结果应为 json。
我收到此错误:
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
最佳答案
基于 OneSignal 的代码 webpage您的请求应该是:
uri = URI.parse("https://onesignal.com/api/v1/apps")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(
uri.path,
"Content-Type" => "application/json",
"Authorization" => "Basic NGXXX...XXXBlNjJj"
)
response = http.request(request)
puts response.body
在您的情况下,错误与不使用 http.use_ssl = true
相关,但您还需要添加其他一些内容,例如授权 header 。
关于ruby-on-rails - RAILS 使用 API key RESTApi 向 onesignal 发送 get 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32564952/