【问题标题】:HTTParty options parameter not functioning properlyHTTParty 选项参数无法正常工作
【发布时间】:2013-05-26 13:56:01
【问题描述】:

我正在设置一个可以发出 LastFM API 请求的应用程序。 这些是简单的 get 请求,我正在使用 HTTParty gem。

我的功能如下:

def get_albums
  self.class.base_uri "http://ws.audioscrobbler.com/2.0/"
  options = {
    :user => "Gerard1992",
    :method => "user.gettopalbums", 
    :api_key => Constants::LASTFM_API_KEY, 
    :format => "json"
  }
  puts options.to_query
  self.class.get "/?#{options.to_query}", {} #options don't work
end

上面显示的这段代码有效。 get 请求返回一组 JSON。我的问题是这个/?#{options.to_query} 看起来不那么整洁。实际的(现在为空的{})选项参数也没有。如何让 HTTParty 选项参数正常工作?

这是我尝试过的,但两种情况都失败了:

self.class.get "/", options
self.class.get "/", options => options

感谢您的帮助。

【问题讨论】:

    标签: ruby httparty


    【解决方案1】:

    HTTParty中查询参数的正确选项是:query,所以你想要的是:

    self.class.get "/", query: options
    

    您可以在the docs 中查看所有可用参数。

    【讨论】:

    • 我觉得这很奇怪。我也从 HTTParty gem 切换到 Rest-client gem,因为 HTTParty 使用'self.class。
    【解决方案2】:

    在选项哈希中发送 :verify => false

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多