Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to implement an OAuth provider in Rails 3. When I try to authorize a client app I get this error. I am using the RESTful auth plugin and pelles OAuth-plugin. When I was testing via the Rails console and getting this error I thought that I simply needed to encode the URLs but I get the same error when testing in browser so I am not sure what is wrong.

like image 283
p01nd3xt3r Avatar asked Mar 21 '11 06:03

p01nd3xt3r


People also ask

Is it legal to parse a URI that is not a URI?

URI.parse is right: that URI is illegal. Just because it accidentally happens to work in your browser doesn't make it legal. You cannot parse that URI, because it isn't a URI. Show activity on this post.

What characters should I encode with Uri encode ()?

There are several characters that you should encode with URI::encode (). For instance, #, , or & are not valid in a query.

How to check if a URL is valid or not?

Check your url is not nil, and it's a valid one. Do URI.encode (url) before URI.parse (to avoid special characters) Do strip to the string you pass to URI.parse (to avoid leading and trailing whitespaces).

What is a better replacement for Uri in Gemfile?

Addressable::URI is a better, more rfc-compliant replacement for URI: gem install addressable first. Show activity on this post. Show activity on this post.


2 Answers

I got into trouble with URI.split (returning this error), I don't know if this helps you, but I will post here some warnings for also someone else having this error:

  1. Check your url is not nil, and it's a valid one.
  2. Do URI.encode(url) before URI.parse (to avoid special characters)
  3. Do strip to the string you pass to URI.parse (to avoid leading and trailing whitespaces).

All in one:

uri = URI.parse(URI.encode(url.strip)) 

Related resource: http://www.practicalguile.com/2007/09/15/raising-uriinvalidurierror-from-a-perfectly-valid-uri/

like image 52
jBilbo Avatar answered Sep 19 '22 12:09

jBilbo


You can also use this alternative URI gem: https://github.com/sporkmonger/addressable

There is no such problems with it.

Very native, just add namespace in your code after installing the gem

Addressable::URI 
like image 27
Andrey Skuratovsky Avatar answered Sep 21 '22 12:09

Andrey Skuratovsky