2

它是一个 RESTAPI,我需要向 onesignal 发送获取请求。

网址 = 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>
4

1 回答 1

2

根据 OneSignal网页中的代码,您的请求应该是:

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,但您还有其他几项要添加,例如授权标头。

于 2015-09-14T12:49:48.767 回答