mageofzema
mageofzema

Reputation: 85

Ruby no method error with HTTParty

I am trying to make a post request in Ruby and I am getting inconsistent behaviour. I am currently using ruby-2.0.0-p598 on OSX.

When using PRY and I type the following post command:

HTTParty.post(@base_uri + '/method/?argument1&api_key=' + @api_key)

I get a successful respond from the API. However when I run it through my specs or inside the class I get:

undefined method `+' for nil:NilClass

I know it has to do with the plus sign, but I find it weird that I am getting a different behaviour. Can you please suggest what is the correct way of doing this?

Thanks in advance.

Upvotes: 0

Views: 1412

Answers (2)

Shahzad Tariq
Shahzad Tariq

Reputation: 2857

It looks like @base_uri and/or @api_key is null. Please double check if they are initialized with valid strings or not. Then try

 HTTParty.post("#{@base_uri}/method/?argument1&api_key=#{@api_key}")

In this case, ruby will automatically try to convert @base_uri and @api_key to string so no need to call to_s method explicitly.

Upvotes: 0

diderevyagin
diderevyagin

Reputation: 332

Good day Behavior correct - some variable = nil. You have check variables, or (in this case it is better not to do) call to_s:

HTTParty.post(@base_uri.to_s + '/method/?argument1&api_key=' + @api_key.to_s)

Upvotes: 2

Related Questions