rubyhttphttp-headerscase-sensitivecustom-headers

How to preserve custom headers case in ruby


On a note HTTP spec says that headers are case insensitive, we have a need to call a service which is having the case sensitive headers.

Below is my code

require 'uri'
require 'net/http'
require 'openssl'

url = URI("Service URL")

http = Net::HTTP::Proxy('127.0.0.1', '8888').new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["content-type"] = 'text/xml'
request["charset"] = 'utf-8'
request["accept"] = 'text/xml'
request["host"] = 'abc.com'
request["HMACSignature"] = 'dsfsdfsdf'

response = http.request(request)
puts response.read_body

the custom header is being receiving as Hmacversion.

I have also tried this but it is not working.

Is there any work around for this


Solution

  • If at all possible, I would try to change the server, which is violating HTTP standards by treating request header keys as case-sensitive - "Field names are case-insensitive". That error will mess with browsers, caches and so on.

    If you can't fix it, I would probably try another HTTP client library that preserves case, not Net::HTTP. Just make sure that library doesn't use Net::HTTP behind the scenes. You could try Excon for example (I'm not sure if it preserves case but it has a lot of low-level control).