From e53b0036a7e04ed4ae6c7d30286370473a61c5c3 Mon Sep 17 00:00:00 2001 From: r00tkrnl Date: Sat, 28 Nov 2026 08:37:10 +0300 Subject: [PATCH] Format JSON responses with an uppercase Content-Type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Media type names are case insensitive so `APPLICATION/JSON` has to be formatted like `application/json`. Refs #1958 🤖 Generated with Codebuff Co-Authored-By: Codebuff --- httpie/output/formatters/json.py | 2 ++ tests/test_json.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/httpie/output/formatters/json.py b/httpie/output/formatters/json.py index bc6151e42f..5633a62133 100644 --- a/httpie/output/formatters/json.py +++ b/httpie/output/formatters/json.py @@ -15,6 +15,8 @@ def format_body(self, body: str, mime: str) -> str: 'javascript', 'text', ] + # Media type names are case insensitive, so APPLICATION/JSON is JSON too. + mime = mime.lower() if (self.kwargs['explicit_json'] or any(token in mime for token in maybe_json)): from ..utils import load_prefixed_json diff --git a/tests/test_json.py b/tests/test_json.py index e758ebe7f4..f0b1988206 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -91,6 +91,27 @@ def test_json_formatter_with_body_preceded_by_non_json_data( assert expected_body in r +@pytest.mark.parametrize( + 'content_type', + [ + 'application/json', + 'APPLICATION/JSON', + 'Application/Problem+JSON; charset=utf-8', + ], +) +@responses.activate +def test_json_formatter_ignores_content_type_case(content_type): + """Media type names are case insensitive.""" + responses.add( + responses.GET, + DUMMY_URL, + body='{"message":"ok"}', + content_type=content_type, + ) + r = http('--pretty=format', '--body', DUMMY_URL) + assert '{\n "message": "ok"\n}' in r + + @responses.activate def test_duplicate_keys_support_from_response(): """JSON with duplicate keys should be handled correctly."""