christian christian - 5 months ago 13
Ruby Question

Httparty request query not parsed properly

I would like to pass some query parameters to

HTTParty.get
. I have a helper method to handle requests

def handle_request
begin
response = yield
if response['Success']
response['Payload']
else
raise Bondora::Error::ApiError, "#{response['Errors'][0]['Code']}: #{response['Errors'][0]['Message']}"
end
rescue Net::OpenTimeout, Net::ReadTimeout
{}
end
end


And another method to to make the request:

def investments(*params)
handle_request do
url = '/account/investments'
self.class.get(url, :query => params)
end
end


When I call this method like
investments({"User" => "test"})
I should end up with a GET request to
/account/investments?User=test
.
Unfortunately the request params are not parsed properly and the resulting request looks like this:
/account/balance?[{%22User%22=%3E%22test%22}]


Any clue why this happens? I think it has something to do with the methods I wrote.

Answer

When you declare the method as def investments(*params), params will contain an array of arguments, and want to pass a hash to your get call. So, either drop the asterisk and simply say def investments(params), or use query: params.first later in the method.

Comments