# Net::HTTP and the need for all-capital http header names

**URL:** <https://www.ruby-forum.com/t/net-http-and-the-need-for-all-capital-http-header-names/93002>\
**Category:** Ruby\
**Created:** [April 26, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/net-http-and-the-need-for-all-capital-http-header-names/93002 "2007-04-26T13:49:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Folini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@Christian\_Folini](https://www.ruby-forum.com/u/Christian_Folini)\
**Post date:** [April 26, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/net-http-and-the-need-for-all-capital-http-header-names/93002/1 "2007-04-26T13:49:53Z")

</div>

Hi there

Net:HTTP will downcase http headers like FOO-BAR to Foo-Bar  
upon initialization and upon writing to the socket. While  
this behaviour is consistent with most user agents and servers  
it is the wrong approach for a small application I am developping.  
In fact I want to reissue requests _as is_ and as there are  
all-capital header names out there in the wild, I want to  
reissue those as all-capitals too.

Now I did a hack that is not particularly elegant, but works  
for the time being.

require “net/http”

class Net::HTTP::Get

# Redefinition of initialize\_http\_header

# The original method does a downcase on the header name

# We need it untouched however as we want to repeat a request as is

def initialize\_http\_header(initheader)  
@header = {}  
return unless initheader  
initheader.each do |key, value|  
warn “net/http: warning: duplicated HTTP header: #{key}” if  
key?(key) and $VERBOSE  
@header[key] = [value.strip]  
end  
end

end

class Net::HTTP::Post

# Redefinition of initialize\_http\_header

# The original method does a downcase on the header name

# We need it untouched however as we want to repeat a request as is

def initialize\_http\_header(initheader)  
@header = {}  
return unless initheader  
initheader.each do |key, value|  
warn “net/http: warning: duplicated HTTP header: #{key}” if  
key?(key) and $VERBOSE  
@header[key] = [value.strip]  
end  
end

end

class Net::HTTPGenericRequest

# Redefinition of write\_header

# The original one does a downcase on the header name

# We need it untouched however as we want to repeat a request as is

# Furthermore there is a duplication of certain headers

# introduced by our removal of the downcasing in

initialize\_http\_header  
def write\_header(sock, ver, path)  
buf = “#{@method} #{path} HTTP/#{ver}\r\n”  
each do |k,v|  
unless k == “host” or k == “content-length” or k == “content-type”  
# due to our removal of the downcase code, some headers were  
reintroduced by  
# the module. We ignore these.  
buf \<\< “#{k}: #{v}\r\n”  
else  
end  
end  
buf \<\< “\r\n”  
sock.write buf  
end  
end

Obviously, I would perfer to use a better approach, but I am far  
from being a ruby wizard and I do not know much about network  
sockets programming. So is there any resource I could be pointed  
to or is there an alternative way to use Net::HTTP

regs,

Christian

---

<div class="post-metadata">

**Author:** ![Christian\_Folini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@Christian\_Folini](https://www.ruby-forum.com/u/Christian_Folini)\
**Post date:** [November 8, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/net-http-and-the-need-for-all-capital-http-header-names/93002/2 "2007-11-08T17:22:05Z")

</div>

Hi,

I also designing an application where I want to use http headers without  
ruby automatically changing the casing of my headers. Does anyone know  
of a clean solution without redefining Net::HTTP functions? I would  
like to avoid redefining any library functions.

Thanks,  
Edward
