# User Agent String

**URL:** <https://www.ruby-forum.com/t/user-agent-string/133431>\
**Category:** Ruby\
**Created:** [March 31, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/user-agent-string/133431 "2008-03-31T16:30:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [March 31, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/user-agent-string/133431/1 "2008-03-31T16:30:50Z")

</div>

Hey guys,

I want my Ruby script to make a request to a server but I don’t want it  
to use just the basic User Agent String. How would I go about giving my  
script a User Agent string to use?

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 1, 2008, 6:57am UTC](https://www.ruby-forum.com/t/user-agent-string/133431/2 "2008-04-01T06:57:53Z")

</div>

Tom A. wrote:

> Hey guys,
> 
> I want my Ruby script to make a request to a server but I don’t want it  
> to use just the basic User Agent String. How would I go about giving my  
> script a User Agent string to use?

I would suggest posting what you already have and we’ll go from there.

- Mac

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 1, 2008, 8:05am UTC](https://www.ruby-forum.com/t/user-agent-string/133431/3 "2008-04-01T08:05:30Z")

</div>

Hi,

How about:

require ‘net/http’  
user\_agent = “you can set this part”  
h = Net::HTTP.new(‘[www.example.com](http://www.example.com)’, 80)  
resp, data = h.get(‘/index.html’, {“User-Agent”=\>user\_agent})

Zaki

Tom A. wrote:

> Hey guys,
> 
> I want my Ruby script to make a request to a server but I don’t want it  
> to use just the basic User Agent String. How would I go about giving my  
> script a User Agent string to use?

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 11, 2008, 12:09am UTC](https://www.ruby-forum.com/t/user-agent-string/133431/4 "2008-04-11T00:09:00Z")

</div>

On Apr 10, 2008, at 07:56 AM, Ben A. wrote:

> and all it gives me is:  
> SocketError: getaddrinfo: No address associated with nodename

Net::HTTP#initialize takes a host and a port, not URI and port:

$ ruby  
require ‘net/http’  
h = Net::HTTP.new ‘[google.com](http://google.com)’, ‘http’  
resp, data = h.get ‘/’, “User-Agent” =\> “Ruby/#{RUBY\_VERSION}”  
p resp, data  
#\<Net::HTTPOK 200 OK readbody=true\>  
“[…]\<input name=btnI type=submit value="I’m Feeling Lucky"\>[…]”  
$

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 11, 2008, 1:18am UTC](https://www.ruby-forum.com/t/user-agent-string/133431/5 "2008-04-11T01:18:13Z")

</div>

Eric H. wrote:

> On Apr 10, 2008, at 07:56 AM, Ben A. wrote:
> 
> > and all it gives me is:  
> > SocketError: getaddrinfo: No address associated with nodename
> 
> Net::HTTP#initialize takes a host and a port, not URI and port:
> 
> $ ruby  
> require ‘net/http’  
> h = Net::HTTP.new ‘[google.com](http://google.com)’, ‘http’  
> resp, data = h.get ‘/’, “User-Agent” =\> “Ruby/#{RUBY\_VERSION}”  
> p resp, data  
> #\<Net::HTTPOK 200 OK readbody=true\>  
> “[…]\<input name=btnI type=submit value="I’m Feeling Lucky"\>[…]”  
> $

Putting that into irb gives me:  
=\> [#\<Net::HTTPBadRequest 400 Bad Request readbody=true\>, “\n\n400 Bad  
Request\n\n

# Bad Request
\nYour browser sent  
a request that this server could not understand.

\nClient sent  
malformed Host header

\n\n”]

I’ve had some luck with open-uri, but I still want to get the error  
code’s from the site. I would really like to be able to create generic  
HTTP requests, but it’s quite daunting.

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 10, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/user-agent-string/133431/6 "2008-04-10T16:56:05Z")

</div>

Zoltan D. wrote:

> Hi,
> 
> How about:
> 
> require ‘net/http’  
> user\_agent = “you can set this part”  
> h = Net::HTTP.new(‘[www.example.com](http://www.example.com)’, 80)  
> resp, data = h.get(‘/index.html’, {“User-Agent”=\>user\_agent})
> 
> Zaki
> 
> Tom A. wrote:
> 
> > Hey guys,
> > 
> > I want my Ruby script to make a request to a server but I don’t want it  
> > to use just the basic User Agent String. How would I go about giving my  
> > script a User Agent string to use?

I’ve tried the above with the following:  
user\_agent = ‘Ruby Net::HTTP’  
print “Getting h\n”  
h = Net::HTTP.new(‘[http://www.google.com](http://www.google.com)’, 80)  
print “getting response\n”  
resp, data = h.get(‘/’, {“User-Agent”=\>user\_agent})  
print data.body  
print resp  
print “should’ve printed\n”

and all it gives me is:  
SocketError: getaddrinfo: No address associated with nodename

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 request in http.rb at line 1032  
method get in http.rb at line 769  
at top level in rubyheader.rb at line 12
