# Webrick override HTTPResponse::setup\_header

**URL:** <https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146>\
**Category:** Ruby\
**Created:** [April 18, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146 "2008-04-18T12:41:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146/1 "2008-04-18T12:41:17Z")

</div>

Hi, all.

My subj solution:

class MyServlet \< HTTPServlet::AbstractServlet  
def do\_GET(req, res)  
class \<\<res  
def setup\_header()  
… my code …  
end  
end  
…  
end  
…  
end

I think my solution is’t good and is’t true ruby way.  
Another solution?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146/2 "2008-04-18T22:30:28Z")

</div>

On Apr 18, 2008, at 03:40 AM, andrey wrote:

> end  
> …  
> end
> 
> I think my solution is’t good and is’t true ruby way.  
> Another solution?

What are you trying to accomplish with this code?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 19, 2008, 4:10am UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146/3 "2008-04-19T04:10:36Z")

</div>

Hi,

As Eric said, it’s not obvious what end result you are expecting but  
it appears you want to change the headers of the response. If so, you  
can do something like this:

class MyServlet \< HTTPServlet::AbstractServlet  
def do\_Get(req, res)  
res[“header\_name”] = “header\_value”  
…  
end  
end

Webrick is not very well documented but the method names and their  
source code are available here: [RDoc Documentation](http://ruby-doc.org/stdlib/)

Dan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2008, 9:47am UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146/4 "2008-04-20T09:47:41Z")

</div>

Hi andrey,

To clear out all of the existing headers you can use  
res.instance\_variable\_set("@header", {“header\_name” =\> “header\_value”,  
“other\_header\_name” =\> “other\_header\_value”})

Dan

On Sat, Apr 19, 2008 at 10:28 AM, Andrey Nikitin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2008, 9:14am UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146/5 "2008-04-20T09:14:23Z")

</div>

Hi,

Generally you should reply to the ruby-talk list instead of one  
particular person as someone on the list might have more knowledge of  
this subject.

Are you sure that is the behavior of res[“header”]? The source code  
does not look like it differentiates between pre-existing fields and  
new fields. If you want to change the body of the response, use  
res.body = “…”

Dan

2008/4/19 andrey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2008, 10:13am UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146/6 "2008-04-20T10:13:25Z")

</div>

On 19 ÁÐÒ, 18:44, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> To clear out all of the existing headers you can use  
> res.instance\_variable\_set(“@header”, {“header\_name” =\> “header\_value”,  
> “other\_header\_name” =\> “other\_header\_value”})

Yes, but after exiting do\_Get() Webrick add self headers:

```
 def do_GET(req, res)
    ...
     res.instance_variable_set("@header",
                              {"header_name" => "header_value",
                              "other_header_name" =>

```

“other\_header\_value”})  
…  
end

% nc localhost 60001  
HEAD /mjpeg.cgi HTTP/1.0

HTTP/1.1 200 OK  
Connection: close  
Date: Sat, 19 Apr 2008 14:52:47 GMT  
Content-Type:text/html  
Server: WEBrick/1.3.1 (Ruby/1.8.5/2006-08-25)  
Other\_header\_name: other\_header\_value  
Header\_name: header\_value

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2008, 3:05am UTC](https://www.ruby-forum.com/t/webrick-override-httpresponse-setup-header/135146/7 "2008-04-23T03:05:57Z")

</div>

- andrey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (2008-04-19) schrieb:

> On 19 Ð°Ð¿Ñ€, 18:44, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > To clear out all of the existing headers you can use  
> > res.instance\_variable\_set(“@header”, {“header\_name” =\> “header\_value”,  
> > “other\_header\_name” =\> “other\_header\_value”})
> 
> Yes, but after exiting do\_Get() Webrick add self headers:

If you want to change the Content-Type, use

req[‘Content-Type’] = ‘application/x-my-world’

mfg, simon … l
