Force HTTP header name lowercase

According to RFC 2616 Section 4.2, HTTP headers are case-insensitive. It is up to the HTTP client to send "Content-Type" or "content-type". For example, net/http will send the header as 'Content-Type'.

However, there are some services that do not follow this rule and insist on a specific case. E.g. all lowest case. As a walkaround for this issue, we will create a subclass of string and override the capitalize method.

class StringDisableCapitalize < String
  def capitalize
    self
  end
end

When definition the header name, we create the header name using this class instead of a string or a symbol. Example for HTTparty:

result = get("url",
 :headers => {
     'Content-Type' => 'application/json',
     StringDisableCapitalize.new('my-lowercase-header') => 'value',
 }
)

This will prevent HTTParty (which uses net/http underlying) from capitalizing the header.


AI Summary
Chrome On-device AI 2024-07-20 05:20:17

Share Article