# Setting HTTP Headers

**URL:** <https://www.ruby-forum.com/t/setting-http-headers/62748>\
**Category:** Ruby\
**Created:** [June 7, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/setting-http-headers/62748 "2006-06-07T20:29:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 7, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/setting-http-headers/62748/1 "2006-06-07T20:29:36Z")

</div>

Hi All

I’ve read the Pickaxe and the Ruby Docs but for the life of me I can’t  
figure out an easy way of setting the headers of my HTTP  
request/connection. I see a class called HTTPHeader and I see some  
methods  
have an “initheader” parameter but I was expecting there to be a simple  
way  
of ‘adding’ headers at any point in the script. Possible switching them  
and  
replacing with other values.

Does anyone know of a way?

Many thanks for your help.

Doug

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 7, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/setting-http-headers/62748/2 "2006-06-07T21:22:43Z")

</div>

- Doug B. (removed\_email\_address@domain.invalid) wrote:

> Hi All
> 
> I’ve read the Pickaxe and the Ruby Docs but for the life of me I can’t  
> figure out an easy way of setting the headers of my HTTP  
> request/connection. I see a class called HTTPHeader and I see some methods  
> have an “initheader” parameter but I was expecting there to be a simple way  
> of ‘adding’ headers at any point in the script. Possible switching them and  
> replacing with other values.

“By adding headers at any point in the script?” Just to be sure, we talk  
about http?

I think its not that problem to hack somethink like you want. But if  
in pure ruby, just do it yourself 🙂

> Many thanks for your help.
> 
> Doug

## So far

Michael Trommer [http://ackro.com](http://ackro.com)  
[http://polis.ackro.org](http://polis.ackro.org)  
~  
The reason ESP, for example, is not considered a viable topic in  
contemoprary  
psychology is simply that its investigation has not proven  
fruitful…After  
more than 70 years of study, there still does not exist one example of  
an ESP  
phenomenon that is replicable under controlled conditions. This simple  
but  
basic scientific criterion has not been met despite dozens of studies  
conducted  
over many decades…It is for this reason alone that the topic is now of  
little  
interest to psychology…In short, there is no demonstrated phenomenon  
that  
needs explanation.  
– Keith E. Stanovich, “How to Think Straight About Psychology”, pp.  
160-161

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 7, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/setting-http-headers/62748/3 "2006-06-07T21:29:01Z")

</div>

On Wednesday 07 June 2006 19:27, Doug B. wrote:

> Hi All
> 
> I’ve read the Pickaxe and the Ruby Docs but for the life of me I can’t  
> figure out an easy way of setting the headers of my HTTP  
> request/connection. I see a class called HTTPHeader and I see some methods  
> have an “initheader” parameter but I was expecting there to be a simple way  
> of ‘adding’ headers at any point in the script. Possible switching them  
> and replacing with other values.  
> There’s an example for setting headers using open-uri in the ruby docs:  
> [http://ruby.outertrack.com/file/ruby-1.8.4%2Flib%2Fopen-uri.rb](http://ruby.outertrack.com/file/ruby-1.8.4%2Flib%2Fopen-uri.rb)

open(“[Ruby Programming Language](http://www.ruby-lang.org/en/)”,  
“User-Agent” =\> “Ruby/#{RUBY\_VERSION}”,  
“From” =\> “removed\_email\_address@domain.invalid”,  
“Referer” =\> “[http://www.ruby-lang.org/](http://www.ruby-lang.org/)”) {|f|  
…  
}

If you’re using Net:HTTP directly, initheader is a hash of header  
values. It’s  
really not documented too well there. Unless you feel you need to use  
Net:HTTP, I recommend you use open-uri instead, it does a lot more work  
for  
you. I’m not sure what you mean by the last part of your question, but  
you  
can certainly store your hash in a variable and modify it during your  
script’s execution as necessary.

> Many thanks for your help.  
> I hope I’ve gone some way to answering your question.

> Doug  
> Alex

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 7, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/setting-http-headers/62748/4 "2006-06-07T21:54:55Z")

</div>

On Wednesday 07 June 2006 20:37, Dan D. wrote:

> > … It’s really not documented too well there.
> 
> Can open-uri do POSTs? I am pretty sure the answer is no but I would like a  
> definitive answer from someone who is very familiar with open-uri.  
> [http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/)  
> Documentation may be lacking in some areas, but you can rely on RDoc to  
> at  
> least generate a list of all public methods. I’m afraid OpenURI is  
> read-only “for the moment”  
> [http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/classes/OpenURI/OpenRead.html#M000783](http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/classes/OpenURI/OpenRead.html#M000783)

Wanting to do POST requests might be a good reason to use Net:HTTP open  
open-uri 🙂

Alex

---

<div class="post-metadata">

**Author:** ![ashiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e5b9ba/32.png) [@ashiro](https://www.ruby-forum.com/u/ashiro)\
**Post date:** [June 7, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/setting-http-headers/62748/5 "2006-06-07T21:38:40Z")

</div>

> … It’s really not documented too well there.

Can open-uri do POSTs? I am pretty sure the answer is no but I would  
like a definitive answer from someone who is very familiar with  
open-uri.
