Question

I'm using the HTTParty gem to post a request on another website of mine. In my dedicated class I define some defaults according to HTTParty's documentation:

require 'httparty'

class Notifier

  include HTTParty
  base_uri "my_awesome_site.com"
  basic_auth "an_awesome_client", "megasecretkey"

  ...

end

Then within such class I have my notify method posting the actual request:

def notify
  ...
  result = HTTParty.post( '/receiver.json', 
                         :query => some_message_of_mine_as_a_hash)
  ...
end

Everything seems to be set up correctly, yet the connection fails. Debugging the method I find out that at the moment of the request HTTParty's default_options Hash is empty. What's going on?

Was it helpful?

Solution

You have included the HTTParty module in your Notifier class. This makes the methods defined within that module available on your class. When you call base_uri and base_auth, you're setting configuration on Notifier, not within HTTParty itself.

In your notify method you should be calling post directly instead of against HTTParty

result = post( '/receiver.json', 
               :query => some_message_of_mine_as_a_hash )

See the very last line of this example. Notice, the call from outside the class is on Partay.post, not Partay.HTTParty.post.

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