Hommer Smith
Hommer Smith

Reputation: 27852

Ruby - Invalid URI message

I have a URI like this:

https://chart.googleapis.com/chart?cht=lc&chd=s:cEAELFJHHHKUju9uuXUc&chco=76A4FB&chls=2.0&chs=220x125&chxt=x,y&chxr=1,0,4&chxl=3:|Jan|Feb|Mar&chxs=2,0000dd,13,-1,t,FF0000&chxp=2,10,35,95&chxtc=1,5,15

And I try this:

require 'open-uri'
open "https://chart.googleapis.com/chart?cht=lc&chd=s:cEAELFJHHHKUju9uuXUc&chco=76A4FB&chls=2.0&chs=220x125&chxt=x,y&chxr=1,0,4&chxl=3:|Jan|Feb|Mar&chxs=2,0000dd,13,-1,t,FF0000&chxp=2,10,35,95&chxtc=1,5,15" 

And I get the following message:

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

What should I do?

Upvotes: 1

Views: 616

Answers (2)

iltempo
iltempo

Reputation: 16012

The pipes are causing your problem. They have to be percent encoded.

You may work around this by

uri = "https://chart.googleapis.com/chart?cht=lc&chd=s:cEAELFJHHHKUju9uuXUc&chco=76A4FB&chls=2.0&chs=220x125&chxt=x,y&chxr=1,0,4&chxl=3:|Jan|Feb|Mar&chxs=2,0000dd,13,-1,t,FF0000&chxp=2,10,35,95&chxtc=1,5,15" 
uri.gsub!('|', '%7C')
open uri

Upvotes: 3

ksol
ksol

Reputation: 12235

Did you try url-encoding the string with CGI::escape(str) ?

Upvotes: 1

Related Questions