From 77b8ae0dadb17287aaaeaef33628bc11bda72139 Mon Sep 17 00:00:00 2001 From: Parin Porecha Date: Sat, 18 Apr 2026 12:01:27 +0000 Subject: [PATCH] fix: set Content-Type: application/json when auto-detected with single header. Fixes #1640. --- httpie/client.py | 3 +-- tests/test_defaults.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/httpie/client.py b/httpie/client.py index a1da284a7c..25077f9c51 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -268,8 +268,7 @@ def make_default_headers(args: argparse.Namespace) -> HTTPHeadersDict: auto_json = args.data and not args.form if args.json or auto_json: default_headers['Accept'] = JSON_ACCEPT - if args.json or (auto_json and args.data): - default_headers['Content-Type'] = JSON_CONTENT_TYPE + default_headers['Content-Type'] = JSON_CONTENT_TYPE elif args.form and not args.files: # If sending files, `requests` will set diff --git a/tests/test_defaults.py b/tests/test_defaults.py index dc91211649..063d2bbd08 100644 --- a/tests/test_defaults.py +++ b/tests/test_defaults.py @@ -130,3 +130,15 @@ def test_print_overridable_when_stdout_redirected(self, httpbin): env = MockEnvironment(stdin_isatty=True, stdout_isatty=False) r = http('--print=h', 'GET', httpbin + '/get', env=env) assert HTTP_OK in r + + +def test_json_content_type_with_single_header(httpbin): + """ + Test that Content-Type: application/json is set when a single header is present. + https://github.com/httpie/cli/issues/1640 + """ + r = http('--print=H', 'POST', httpbin + '/post', 'header1: xyz', 'x=1') + assert 'Content-Type: application/json' in r + # Verify it matches behavior with multiple headers + r2 = http('--print=H', 'POST', httpbin + '/post', 'header1: xyz', 'header2: abc', 'x=1') + assert 'Content-Type: application/json' in r2