【问题标题】:HTTP request: 400 errorHTTP 请求:400 错误
【发布时间】:2016-11-18 02:55:36
【问题描述】:

对于我在 Rails 中开展的项目,我使用的是 AlchemyData 新闻 API。我目前正在尝试在 pry 中运行 HTTP 请求,如下所示(我用替代品 my_api_key 替换了我自己的 API 密钥,为了清楚起见,> 表示我输入的内容,=> 是什么已退还给我):

> 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>

我不明白为什么我不断收到“400 Bad Request”错误。据我了解,这(通常)是在 url 出现错误时返回的,但是当我运行我的时它工作得很好。我的语法有问题还是有其他错误?如果是这样,它是什么?我该如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails http http-status-code-400 alchemyapi


    【解决方案1】:

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

    result = HTTP.get("my_url_here")

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多