stop ruby http request modifying header name

http, ruby

Solution

According to the HTTP spec (RFC 2616), header field names are case-insensitive. So the third-party server has a broken implementation.

If you really needed to, you could monkey-patch Net::HTTP to preserve case, because it downcases the field names when it stores them and then writes them with initial caps.

Here's the storage method you used (`Net::HTTPHeader#[]=`):

# File net/http.rb, line 1160
    def []=(key, val)
      unless val
        @header.delete key.downcase
        return val
      end
      @header[key.downcase] = [val]
    end

And here is where it writes the header (`Net::HTTPGenericRequest#write_header`):

# File lib/net/http.rb, line 2071
    def write_header(sock, ver, path)
      buf = "#{@method} #{path} HTTP/#{ver}\r\n"
      each_capitalized do |k,v|
        buf << "#{k}: #{v}\r\n"
      end
      buf << "\r\n"
      sock.write buf
    end

Those are likely the only methods you'd need to override, but I'm not 100% certain.

Problem

I'm doing an http request in ruby: ``` http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Post.new(uri.path) req.body = payload req['customeheader'] = 'xxxxxxxxx' http.set_debug_output $stdout ``` I have debug switched on and when the request is posted I can see the header is being posted as: ``` Customheader: xxxxxxxxx ``` Is there anyway to stop this, the third party server I'm posting to is giving an error because the header name isn't correct - it's expecting `customheader:`

Original source