# Problems trying to feed a user agent header to Net::HTTP.get

**URL:** <https://www.ruby-forum.com/t/problems-trying-to-feed-a-user-agent-header-to-net-http-get/58444>\
**Category:** Ruby\
**Created:** [April 21, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/problems-trying-to-feed-a-user-agent-header-to-net-http-get/58444 "2006-04-21T19:41:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [April 21, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/problems-trying-to-feed-a-user-agent-header-to-net-http-get/58444/1 "2006-04-21T19:41:38Z")

</div>

Say, I thought I had this working at one point, but I don’t now seem to  
be  
able to feed a user agent header to the get\_response, nor the get method  
now.  
Presumably I was mistaken to believe I ever had it. Can someone make a  
suggestion as to how I can feed an arbitrary HTTP header (most  
especially a  
UAH) to these methods? Thanks. My present code is as follows:

#!/usr/bin/ruby

require ‘net/http’  
require ‘uri’  
def fetch(uri\_str, limit=10)  
fail ‘http redirect too deep’ if limit.zero?  
puts “Trying: #{uri\_str}”  
begin  
uah = \<\<EOUAH  
Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.7.10) Gecko/20050716  
Firefox/1.0.6  
EOUAH  
response = Net::HTTP.get\_response(URI.parse(uri\_str),{‘User-Agent’  
=\> uah})  
case response  
when Net::HTTPSuccess  
return response  
when Net::HTTPRedirection  
response = fetch(response[‘location’],limit-1)  
return response

# else

# response.error!

```
end

```

rescue Timeout::Error, EOFError =\> kapow  
STDERR.print “trace Net::HTTP::get failure: #{uri\_str}\n#{@uah}\n”  
STDERR.print “trace Net::HTTP::get tc: #{tryCount}\n”  
STDERR.print “trace Net::HTTP::get kapow: #{kapow}\n”  
end  
end  
response = fetch(‘[http://www.stage.rhapsody.com/genre?genreId=115](http://www.stage.rhapsody.com/genre?genreId=115)’)  
p response.body[500,55]

I’ve tried several variasious on this, but this modification of the  
Pickaxe  
example illustrates most closely the set of things I’m trying to  
accomplish.  
Seems to me this should be pretty simple, right? Also, here is my ruby  
-v:

ruby 1.8.3 (2005-06-23) [i486-linux]  
xc

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [April 21, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/problems-trying-to-feed-a-user-agent-header-to-net-http-get/58444/2 "2006-04-21T19:47:56Z")

</div>

I had a similar issue lately (not sure it is similar): I wanted to feed  
extra headers when redirected.  
I use http-access2 instead of net/http  
Here’s a sample code that worked for me.

#!/usr/bin/ruby

require ‘http-access2’  
require ‘pp’

target = ARGV.shift || “[http://localhost/foo.cgi](http://localhost/foo.cgi)”

clnt = HTTPAccess2::Client.new  
clnt.debug\_dev = STDERR

extheaders = []

clnt.set\_cookie\_store(“cookie.dat”)  
clnt.redirect\_uri\_callback = lambda { |res|  
uri = res.header[‘Location’][0]  
if res.header[‘set-cookie’]  
res.header[‘set-cookie’].each do |cookie|  
puts cookie  
puts uri  
extheaders \<\< [‘Cookie’, cookie]  
#bug in cookie manager, does not work, the uril for the cookie  
misses the first element pof the path

# clnt.cookie\_manager.parse(cookie, URI.parse(uri))

```
    end

```

end  
clnt.save\_cookie\_store  
pp clnt.cookie\_manager  
URI.parse(uri)  
}

puts  
puts ‘= GET content directly’  
puts clnt.get\_content(target, nil, extheaders)  
puts ‘= GET result object’  
result = clnt.get(target)  
puts ‘== Header object’  
p result.header  
puts “== Content-type”  
p result.contenttype  
puts ‘== Body object’  
p result.body  
puts ‘== Content’  
print result.content  
puts ‘== GET with Block’  
clnt.get(target) do |str|  
puts str  
end  
clnt.save\_cookie\_store
