Question

I am trying to use HTTParty to perform an HTTP request inside one Sinatra app. I can perform the same request successfully outside the Sinatra app, but when it is performed within the app, I always get the same exception:

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

The rest of the stack trace is irrelevant. I am sure that the request ends successfully. The exact code that performs the request is as follows:

@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})

It looks like Sinatra is trying to rescue some exception, and then calls a status method on the result of the HTTP request I performed with HTTParty.

So, it would be great to understand what exactly is happening and why would Sinatra even do so.

Thank you very much in advance for your responses.

Was it helpful?

Solution

The problem was in the name of the variable used to store the response: @response is reserved for Sinatra's internal purposes. Thanks to @eljojo for pointing me to this.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top