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 AI Summary
gpt-4.1-2025-04-14 2025-06-17 23:59:37
Although HTTP headers are case-insensitive by standard, some services require all-lowercase headers. This article discusses a workaround for such scenarios by creating a custom string subclass in Ruby to prevent automatic capitalization of header names, specifically when using the HTTParty library.
Chrome On-device AI 2025-06-17 23:59:41
Writing

Share Share this Post