Repository navigation
Description
We've identified an issue in the current release of net-http (0.3.2).
When setting a 'Content-Type' header on a POST request like so:
request['Content-Type'] = 'application/json'
It does set the header, however, it does not recognize that the Content-Type is set correctly and adds an additional header to the request with the default application/x-www-form-urlencoded
This can be seen when calling: request.to_hash.inspect:
{"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "Accept"=>["*/*"], "User-Agent"=>["Ruby"], "Host"=>["api.songstats.com"], "Content-Type"=>["application/json"], "connection"=>["close"], "host"=>["api.songstats.com"], "content-length"=>["290"], "content-type"=>["application/x-www-form-urlencoded"]}
The only way to force the application/json header to go through is to explicitly set it in the request:
request.content_type = 'application/json'
So actually it needs to be set twice in order to fully work:
request['Content-Type'] = 'application/json'
request.content_type = 'application/json'
Also when setting request['content-type'] = 'application/json' in lower case it's throwing the error:
NoMethodError: undefined method `split' for nil:NilClass
from /Users/Oskar/.rbenv/versions/3.2.1/lib/ruby/3.2.0/net/http/header.rb:713:in `main_type'
It would be great if this can be streamlined so that setting the content-type in the headers immediately propagates across the entire request, without the need to set it multiple times or be cautious of case sensitivity. This took us a couple of hours to debug because we were un-aware that it currently sends the same header twice in the same request with different values.
Activity
altruby commentedon Sep 19, 2023
@OskarEichler
Can you provide an isolated reproduction case ? Are you using
set_form,set_form_data?I tried to reproduce, but no luck.
OskarEichler commentedon Sep 19, 2023
@0x1eef Thanks for looking into this - here is how we send the request:
Also note that we had to create custom methods
CaseSensitiveGetandCaseSensitivePostas you library for some reason Capitalized the header keys. In the case of amazon they do not recognize the key 'X-api-key', only 'x-api-key', so we had to manually create the modifications so that they don't get auto cased. It would be great if you can fix that so that the keys are passed exactly as they are being provided by the user.Here is our CaseSensitiveGet class for context:
osyoyu commentedon May 11, 2026
I also couldn't repro the original issue on both Ruby 3.2.2 + net-http 0.3.2 & Ruby 4.0 + net-http HEAD.
@OskarEichler if this is still a problem for you, could you provide repro code?
https://httpbin.org/headersis a useful endpoint for demonstration.Regarding #148 (comment), HTTP/1.1 headers are defined as case-insensitive in RFC 9110. Server implementations should treat
X-api-keyandx-api-key(andX-Api-Key, which is what net/http produces) equal. All-lowercase headers are a HTTP/2 thing.OskarEichler commentedon Oct 2, 2026
Thanks for checking this. I re-ran an isolated reproduction and the duplicate header comes from the custom
CaseSensitivePost/CaseSensitiveGetoverrides shown above, not stockNet::HTTP.With Ruby 4.0.6 and net-http 0.3.2, a normal
Net::HTTP::Postreportscontent_type == "application/json"and serializes exactly one Content-Type header. The custom subclass stores"Content-Type"without normalizing the internal key, socontent_typereturns nil; serialization then adds the lowercase default and produces the two headers from the original report. This was verified against a StringIO-backed output, without sending a network request. With net-http 0.9.1 the same broken subclass still reports nil, but this particular duplicate-header serialization no longer reproduces.So the original report incorrectly attributed our subclass's broken internal header invariant to net-http. Normalized internal keys must be preserved; changing wire casing must not replace the header lookup/storage contract. Setting Content-Type twice is not required for the stock client. I agree about HTTP field names being case-insensitive, and I am not requesting that net-http change that behavior.