【问题标题】:Using HTTParty crashes Sinatra使用 HTTParty 会导致 Sinatra 崩溃
【发布时间】:2013-09-09 00:46:40
【问题描述】:

我正在尝试使用 HTTParty 在一个 Sinatra 应用程序中执行 HTTP 请求。我可以在 Sinatra 应用程序之外成功执行相同的请求,但是当它在应用程序内执行时,我总是得到相同的异常:

2013-09-08T16:04:44.738207+00:00 app[web.1]: [2013-09-08 16:04:44] ERROR NoMethodError: undefined method 'status=' for #<HTTParty::Response:0x007f17c050dd70>`
2013-09-08T16:04:44.837777+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:225:in `status'
2013-09-08T16:04:44.936617+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1080:in `handle_exception!'
2013-09-08T16:04:45.039513+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1062:in `block in dispatch!'
2013-09-08T16:04:45.139212+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `block in invoke'
2013-09-08T16:04:45.238557+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `catch'
2013-09-08T16:04:45.338714+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1041:in `invoke'
2013-09-08T16:04:45.437306+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1062:in `rescue in dispatch!'
2013-09-08T16:04:45.536146+00:00 app[web.1]:    /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.3/lib/sinatra/base.rb:1069:in `dispatch!'

堆栈跟踪的其余部分无关紧要。我确定请求成功结束。 执行请求的具体代码如下:

@response = HTTParty.get("https://foursquare.com/oauth2/access_token",
           :query => {:client_id => settings.client_id,
                      :client_secret => settings.client_secret,
                      :grant_type => "authorization_code",
                      :redirect_uri => settings.redirect_uri,
                      :code => @code})

看起来 Sinatra 正试图挽救一些异常,然后对我使用 HTTParty 执行的 HTTP 请求的结果调用 status 方法。

所以,很高兴了解到底发生了什么以及 Sinatra 为何会这样做。

非常感谢您的回复。

【问题讨论】:

    标签: ruby sinatra httparty


    【解决方案1】:

    问题在于用于存储响应的变量的名称:@response 是为 Sinatra 的内部目的而保留的。感谢@eljojo 向我指出这一点。

    https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L858

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多