fix: preserve Content-Type when sending JSON data with a single custom header#1856
Open
lingxiu58 wants to merge 1 commit into
Open
fix: preserve Content-Type when sending JSON data with a single custom header#1856lingxiu58 wants to merge 1 commit into
lingxiu58 wants to merge 1 commit into
Conversation
…m header When sending JSON data with exactly one custom header, the Content-Type header was silently dropped. Root cause: apply_missing_repeated_headers() used CIMultiDict.popone()+update() which can lose entries due to fragile CIMultiDict/CaseInsensitiveDict interaction. Fix: rebuild headers from scratch using add() instead of pop+replace, materializing items to lists before iteration. Closes httpie#1834
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When sending a JSON request with exactly one custom header, the
Content-Type: application/jsonheader is silently dropped. With two or more custom headers,Content-Typeis correctly preserved.Reproduce:
In both cases the data IS serialized as JSON, but the server doesn't know to parse it as JSON in the single-header case.
Root Cause
apply_missing_repeated_headers()inclient.pyusesCIMultiDict.popone()+update()to re-add original headers afterrequests.PreparedRequestprocessing. When initialized from aCaseInsensitiveDict(from requests), this pop-and-replace pattern onCIMultiDictcan silently lose entries depending on the internal hash table state — which correlates with the total number of header entries.Fix
Rebuild the headers dict from scratch using
add()instead of the fragilepopone()+update()pattern:new_headersfrom empty — instead ofHTTPHeadersDict(prepared_request.headers)then popping/re-addingadd()instead ofpopone()+update()— avoids the fragile pop-then-replace pattern on CIMultiDictCloses #1834