Ruby: HTTP request path is empty
If you’ve ever tried to grab webpages with Ruby you may have come across the runtine error message HTTP request path is empty
. My acute pyschocosmic intuition tells me that you may have code similar to the following.
The path
piece of the message is a small clue; the error pertains the supplied URL. It seems that http://bing.com
may not be interpreted as a complete URL. How do I know? I Read The Fun Manual.
According to the RFC a valid URL looks like this:
http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
What are we missing? The abs_path
part. It is easy to forget this because modern web browsers are so idiot proof we can simply supply a domain name such as bing.com
and the rest will be taken care of for us.
The solution is to emulate our web browsers and manually append a /
to the end of the URL. If you’re familiar with Unix you’ll notice that this symbol signifies the root of the filesystem.