Skip to content

URI::InvalidURIError (bad URI(is not URI?)

Reena H Rajani edited this page Jun 2, 2015 · 1 revision

Running my application on Heroku generated the following error

URI::InvalidURIError (bad URI(is not URI?): https://api.edamam.com/search?q=chickpea curry&app_id=c5975da7&app_key=5cc84166d69454f54ed43fb1bcb9b858):

reason :

This URL contains special characters, which Ruby can’t handle (my guess)

Solution :

Encode the url and then parse it encoded_url = URI.encode(url.strip) URI.parse(encoded_url)

  • Strip removes the leading and trailing whitespaces
  • URI.encode --> used to avoid the special characters

Example :

url = "https://api.edamam.com/search?q=vegemite,bourbon&app_id=c5975da7&app_key=5cc84166d69454f54ed43fb1bcb9b858"

encoded_url = URI.encode(url.strip) => "https://api.edamam.com/search?q=vegemite,bourbon&app_id=c5975da7&app_key=5cc84166d69454f54ed43fb1bcb9b858"

uri = URI.parse(encoded_url) => #<URI::HTTPS https://api.edamam.com/search?q=vegemite,bourbon&app_id=c5975da7&app_key=5cc84166d69454f54ed43fb1bcb9b858>