3

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

Community
  • 1
  • 1
Srinivas M
  • 303
  • 1
  • 3
  • 12

2 Answers2

2

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).

mahemoff
  • 44,526
  • 36
  • 160
  • 222
0

Use following code to force case sensitive headers.

class CaseSensitivePost < Net::HTTP::Post
  def initialize_http_header(headers)
    @header = {}
    headers.each{|k,v| @header[k.to_s] = [v] }
  end

  def [](name)
    @header[name.to_s]
  end

  def []=(name, val)
    if val
      @header[name.to_s] = [val]
    else
      @header.delete(name.to_s)
    end
  end

  def capitalize(name)
    name
  end
end

Usage example:

post = CaseSensitivePost.new(url, {myCasedHeader: '1'})
post.body = body
http = Net::HTTP.new(host, port)
http.request(post)

Others suggested creating custom string class, that won't get downcased or capitalized. https://stackoverflow.com/a/42121370/979995

Kaplan Ilya
  • 479
  • 3
  • 12