Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop ruby http request modifying header name

Tags:

http

ruby

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:

like image 857
probably at the beach Avatar asked Apr 21 '12 12:04

probably at the beach


1 Answers

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:

class CaseSensitiveString < String
    def downcase
        self
    end
    def capitalize
        self
    end
end

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.

like image 196
Corin Avatar answered Oct 13 '22 20:10

Corin