From: 7stud -- Date: 2008-04-11T19:04:10+09:00 Subject: Re: Ruby HTTP GET Headers Ben Aroia wrote: > I've looked here: 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") 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 2) In open-uri, the open() method allows you to specify headers: require 'open-uri' url = '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 -- Posted via http://www.ruby-forum.com/.