Fix: Ensure Content-Type header is preserved when single custom header is present - #1909
Closed
vinayK34 wants to merge 1 commit into
Closed
Fix: Ensure Content-Type header is preserved when single custom header is present#1909vinayK34 wants to merge 1 commit into
vinayK34 wants to merge 1 commit into
Conversation
…r is present This fixes issue #1834 where the Content-Type: application/json header was not being set when sending a POST request with JSON data and a single custom header. The bug occurred in the header processing logic in apply_missing_repeated_headers function where it was incorrectly removing automatically set Content-Type headers when there was only one custom header. The fix ensures that Content-Type headers that are automatically set for JSON data are preserved during header merging, preventing them from being lost when custom headers are present.
vinayK34
marked this pull request as ready for review
July 28, 2026 18:45
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.
This fixes issue #1834 where the Content-Type: application/json header was not being set when sending a POST request with JSON data and a single custom header.
Problem
When sending a POST request with JSON data and exactly one custom header (e.g.,
https post pie.dev/post -v 'header1: xyz' x=1), theContent-Type: application/jsonheader was not being set in the final request. However, when multiple custom headers were present, the header was correctly set.Root Cause
The issue was in the
apply_missing_repeated_headersfunction inhttpie/client.py. When there's only one custom header, the header merging logic incorrectly removed and re-added headers in a way that interfered with automatically set Content-Type headers for JSON data.Solution
Modified the
apply_missing_repeated_headersfunction to detect when a Content-Type header was automatically set for JSON data and preserve it during header processing, even when custom headers might interfere with the normal header merging process.Testing
This fix ensures that when JSON data is being sent, the Content-Type header is always preserved regardless of the number of custom headers present.