# Ruby HTTP GET Headers

**URL:** <https://www.ruby-forum.com/t/ruby-http-get-headers/134413>\
**Category:** Ruby\
**Created:** [April 10, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/ruby-http-get-headers/134413 "2008-04-10T16:46:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [April 10, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/ruby-http-get-headers/134413/1 "2008-04-10T16:46:42Z")

</div>

I’ve looked here: [http://snippets.dzone.com/posts/show/788](http://snippets.dzone.com/posts/show/788) and I’m not  
really understanding it. I am creating a client for a website, and I  
want to set the User-Agent field of the header I am sending. Is there a  
simple way to do this or is this along the lines of creating custom HTTP  
objects? I’ve also looked into the open-uri class, but I get this error:  
Errno::EHOSTDOWN: Host is down - connect(2)

method initialize in http.rb at line 560  
method open in http.rb at line 560  
method connect in http.rb at line 560  
method timeout in timeout.rb at line 48  
method timeout in timeout.rb at line 76  
method connect in http.rb at line 560  
method do\_start in http.rb at line 553  
method start in http.rb at line 542  
method open\_http in open-uri.rb at line 242  
method buffer\_open in open-uri.rb at line 626  
method open\_loop in open-uri.rb at line 164  
method catch in open-uri.rb at line 162  
method open\_loop in open-uri.rb at line 162  
method open\_uri in open-uri.rb at line 132  
method open in open-uri.rb at line 528  
method open in open-uri.rb at line 30  
at top level in rubyheader.rb at line 8

I attached the test file I am working on. Any help would be great.  
Thanks.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [April 10, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/ruby-http-get-headers/134413/2 "2008-04-10T21:03:53Z")

</div>

Hi Ben,

I’m not sure if your code would work like you expect it to, but I’ve  
written a client library for my Halcyon project you might find  
interesting.

> <https://github.com/mtodd/halcyon/blob/master/lib/halcyon/client.rb>

Note that you won’t need to use JSON.parse for the response data  
(that’s something peculiar to Halcyon) and that you may get errors if  
you don’t remove #to\_mash calls because that’s a dependency fulfilled  
by Merb’s Core Extensions. There may be other dependencies that I  
haven’t thought about that will cause problems, but you should be able  
to determine that from the errors when run.

You can get it working with:

c = Halcon::Client.new(‘[http://google.com:80/](http://google.com:80/)’)  
puts c.get(‘/’)

Matt T.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [April 11, 2008, 12:04pm UTC](https://www.ruby-forum.com/t/ruby-http-get-headers/134413/3 "2008-04-11T12:04:09Z")

</div>

Ben A. wrote:

> I’ve looked here: [http://snippets.dzone.com/posts/show/788](http://snippets.dzone.com/posts/show/788) and I’m not  
> really understanding it. I am creating a client for a website, and I  
> want to set the User-Agent field of the header I am sending. Is there a  
> simple way to do this or is this along the lines of creating custom HTTP  
> objects? I’ve also looked into the open-uri class,

The following both work for me:

1. In net/http, the request\_get() method allows you to specify headers  
for a GET request:

require ‘net/http’  
require ‘uri’

pieces = URI.parse(“[http://localhost/cgi-bin/ruby\_test.rb](http://localhost/cgi-bin/ruby_test.rb)”)  
headers = {“User-Agent” =\> “A really cool browser”}

resp = Net::HTTP::start(pieces.host, pieces.port) do |connection|  
connection.request\_get(pieces.path, headers)  
end

puts resp.body

1. In open-uri, the open() method allows you to specify headers:

require ‘open-uri’

url = ‘[http://localhost/cgi-bin/ruby\_test.rb](http://localhost/cgi-bin/ruby_test.rb)’  
headers = {“User-Agent” =\> “Bad Browser”}

open(url, headers) do |f|  
puts f.read  
end

I tested those scripts by putting the following simple cgi script on my  
server. The cgi script just sends back whatever the User-Agent string  
was in the request:

#!/usr/bin/env ruby

require ‘cgi’

c = CGI.new

puts “Content-type: text/html”  
puts  
puts c.user\_agent
