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:
Building off of a previous answer, this will work for ruby 2.3:
The
to_s
method needs to be added because as of 2.3,HTTP::Header
'scapitalize
method callsto_s
on each header.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#[]=
):And here is where it writes the header (
Net::HTTPGenericRequest#write_header
):Those are likely the only methods you'd need to override, but I'm not 100% certain.
While it's possible to monkey patch Net::HTTPHeader and Net:HTTPGenericRequest to remove the downcase and capitalize, I found a different approach that allows selective case sensitivity rather than forcing everything.
The solution:
Then CaseSensitiveString.new('keyname') to create your keys when they are case sensitive. Use strings/symbols for other keys to maintain existing behaviour. This is much simpler than the monkey patching and works well with the rest-client library as well as Net::HTTP.