Bad URI on heroku when using HTTParty

651 views Asked by At

I currently am getting an the following error when using httparty on heroku.

HTTParty works for the first 11 times but on the 12th time it shows this error.

enter image description here

I am trying to seed data into my database.

When I go to the URL via my browser, it works. I ran the same code in development and it works. I am unsure what to do. Please help!

2

There are 2 answers

2
Amit Suroliya On

You pass invalid URI -

"https://maps.googlemaps.com/maps/api/geocode/json?address=San Fransisco"

address has space in URI.

So, wherever you pass params, Do -

uri_path = https://maps.googlemaps.com/maps/api/geocode/json
params = {address: "San Fransisco",...............}

"#{uri_path}?#{params.to_param}"
1
brayzen On

Amit Suroliya was correct. I was using HTTParty as well and it was working through Curl and Development but would crash in production. Because the HTTParty parameter is the literal URL (as a string), it has to be a flawless URL/URI (meaning no spaces). My bad URI was as follows:

HTTParty.get("http://api.blahblahblah.com/v1/Boards/Search?&limit=79&query=#{query}&filter_date_from=1423353600")

Notice the interpolation query=#{query}"

So if query='Michelle Obama', notice space between Michelle and Obama. Because of interpolation, the HTTParty.get('string') it is incorrect.

Solution:

Replace all whitespaces within your string with +, or you could use %20.

I used query.gsub!(' ', '+')

For more info on whitespace in the URL check it out here: Spaces in URLs?