Skip to content

Fix JSON Content-Type not set when single header present#1842

Open
mitre88 wants to merge 1 commit intohttpie:masterfrom
mitre88:fix-json-content-type-single-header
Open

Fix JSON Content-Type not set when single header present#1842
mitre88 wants to merge 1 commit intohttpie:masterfrom
mitre88:fix-json-content-type-single-header

Conversation

@mitre88
Copy link
Copy Markdown

@mitre88 mitre88 commented May 5, 2026

Summary

When a single custom header is passed with JSON body data, the Content-Type header was not being set correctly.

Problem

The order of header updates was incorrect:

  1. make_default_headers (sets Content-Type for JSON)
  2. base_headers.update()
  3. args.headers.update()

But args.headers.update() was overwriting the auto-detected JSON Content-Type because auto_json and args.json check args.data, which at that point is an empty dict (which is falsy).

Fix

Reordered the header updates:

  1. make_default_headers (sets Content-Type for JSON based on args.data)
  2. headers.update(args.headers) (applies custom headers - user headers cannot override auto-detected Content-Type)
  3. base_headers.update() (if provided)

This ensures Content-Type is correctly set when body data exists, even with a single custom header.

Fixes issue #1834

When a single custom header is passed with JSON body data, the
Content-Type header was being overwritten because headers.update()
was called after base_headers but after args.headers.

The order is now:
1. make_default_headers (sets Content-Type for JSON)
2. headers.update(args.headers) (applies custom headers)
3. base_headers.update() (if provided)

This ensures user-provided headers don't incorrectly override the
auto-detected JSON Content-Type when body data exists.

Fixes issue httpie#1834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant