简体   繁体   中英

HTTP request: 400 error

For a project I'm working on in Rails, I'm using AlchemyData news API. I'm currently trying to run an HTTP request in pry, which looks like below (I replaced my own API key with the substitute my_api_key , and for clarity, a > indicates what I've entered and => what was returned to me):

> uri = URI.parse("https://gateway-a.watsonplatform.net/calls/data/GetNews?apikey=MY_API_KEY&outputMode=json&start=now-24h&end=now&q.enriched.url.title=A[Kim^Kardashian]&q.enriched.url.docSentiment.type=positive")
=> #<URI::HTTPS https://gateway-a.watsonplatform.net/calls/data/GetNews?apikey=MY_API_KEY&outputMode=json&start=now-24h&end=now&q.enriched.url.title=A[Kim^Kardashian]&q.enriched.url.docSentiment.type=positive>

> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP gateway-a.watsonplatform.net:443 open=false>

> response = http.request(Net::HTTP::Get.new(uri.request_uri))
=> #<Net::HTTPBadRequest 400 Bad Request readbody=true>

I'm not understanding why I keep getting a "400 Bad Request" error. From my understanding, that is (usually) returned when there is an error with the url, but when I run mine it works perfectly. Is there something wrong with my syntax or is there a different error? If so, what is it? How can I fix this?

通过使用HTTP gem( https://github.com/httprb/http )并使用它来处理我的HTTP请求来解决此问题,如下所示:

result = HTTP.get("my_url_here")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM