# Net::http

**URL:** <https://www.ruby-forum.com/t/net-http/179629>\
**Category:** Ruby\
**Created:** [November 30, 2009, 9:12pm UTC](https://www.ruby-forum.com/t/net-http/179629 "2009-11-30T21:12:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [November 30, 2009, 9:12pm UTC](https://www.ruby-forum.com/t/net-http/179629/1 "2009-11-30T21:12:35Z")

</div>

Hi,

I know how to print simple website

## – require ‘net/http’ Net::HTTP.get\_print ‘[www.example.com](http://www.example.com)’, ‘/index.html’

I wonder how to print output of searcher for example google?  
I mean for example print output from google with filled search filed  
with ‘text’?

I know that there is a simple on

* * *

```
require 'net/http'
require 'uri'

```

## res = Net::HTTP.post\_form(URI.parse(‘[http://www.google](http://www.google)’), {‘search’=\>‘text’, ‘max’=\>‘50’}) puts res.body

but it dont work as i wanted.

i get:  
/usr/lib/ruby/1.8/net/http.rb:1470:in `initialize’: HTTP request path is  
empty (ArgumentError)

Please can you help me?

Regards  
Beny18241

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [November 30, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/net-http/179629/2 "2009-11-30T22:29:39Z")

</div>

beny 18241 wrote:

> * * *
> 
> ```
> require 'net/http'
> require 'uri'
> 
> ```
> 
> ## res = Net::HTTP.post\_form(URI.parse(‘[http://www.google](http://www.google)’), {‘search’=\>‘text’, ‘max’=\>‘50’}) puts res.body

res = Net::HTTP.post\_form(URI.parse(‘[http://www.google.com](http://www.google.com)’),  
{‘q’=\>‘text’, ‘max’=\>‘50’})

Take a look at the address bar when you search for ‘text’ with google.  
Notice how it has q=text… so ‘search’ would be incorrect I believe.

Regards,

- Mac

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 1, 2009, 2:17am UTC](https://www.ruby-forum.com/t/net-http/179629/3 "2009-12-01T02:17:01Z")

</div>

On Nov 30, 2:12 pm, beny 18241 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> res = Net::HTTP.post\_form(URI.parse(‘[http://www.google](http://www.google)’),  
> {‘search’=\>‘text’, ‘max’=\>‘50’})

You have a few problems here. First, Google’s search won’t accept a  
POST. You have to use GET which has a different syntax. Other search  
engines might take a POST though.

> i get:  
> /usr/lib/ruby/1.8/net/http.rb:1470:in `initialize’: HTTP request path is  
> empty (ArgumentError)

You’re using ‘[http://www.google](http://www.google)’ which isn’t valid. Michael partially  
fixed it by adding ‘.com’, but running that will still yield the same  
error (empty path).

The POST method has to have a path to post to. At the very least,  
you’ll need to use ‘[http://www.google.com/](http://www.google.com/)’ (notice the slash at the  
end). Again, this is assuming the site you’re using will take a POST  
– Google won’t and, even if it did, ‘/’ wouldn’t be the right path  
(it would be ‘/search’).

Based on your example, it looks as though you’ve found the  
documentation for Net::HTTP (at [RDoc Documentation](http://ruby-doc.org/stdlib) ). That  
should have everything you need to get a POST or GET working, but that  
depends on the site you actually want to hit.

Generally, if you run a search and end up with a URL that ends in ‘?  
name1=value1&name2=value2’ (like Google’s ‘&q=hello’), you’ll need  
GET. Otherwise, use POST.

You can also view the source of the search form and find the method  
that is used (e.g. ). If one  
isn’t listed (Google doesn’t list the method), it’s GET.

Finally, if you really want Google results here’s an example of a GET  
that will work:

require ‘net/http’

uri = URI.parse(‘[hello - Google Search](http://www.google.com/search?q=hello)’)

# or: uri = URI::HTTP.build({:host =\> ‘[www.google.com](http://www.google.com)’, :path =\> '/

search’, :query =\> ‘q=hello’})

Net::HTTP.start(uri.host) do |http|  
response = http.get(uri.request\_uri)  
# do something with the response  
end

This is similar to Example 3 in the ‘Getting Document From WWW Server’  
section of the Net::HTTP docs.
