From b5341f097fc7eab1666a3bc88650c34654ce5683 Mon Sep 17 00:00:00 2001 From: xu91102 <169027046+xu91102@users.noreply.github.com> Date: Sat, 26 Sep 2026 23:46:28 +0800 Subject: [PATCH] fix(output): match JSON media types case-insensitively --- docs/README.md | 3 ++- httpie/output/formatters/json.py | 1 + tests/test_json.py | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 51dff51aec..51fd3b54da 100644 --- a/docs/README.md +++ b/docs/README.md @@ -653,6 +653,8 @@ If your command includes some data [request items](#request-items), they are ser You can use `--json, -j` to explicitly set `Accept` to `application/json` regardless of whether you are sending data (it’s a shortcut for setting the header via the usual header notation: `http url Accept:'application/json, */*;q=0.5'`). Additionally, HTTPie will try to detect JSON responses even when the `Content-Type` is incorrectly `text/plain` or unknown. +JSON media types are matched case-insensitively, so `Application/JSON` is formatted the same way as `application/json`. + ### Non-string JSON fields Non-string JSON fields use the `:=` separator, which allows you to embed arbitrary JSON data into the resulting JSON object. @@ -2598,4 +2600,3 @@ BSD-3-Clause: [LICENSE](https://github.com/httpie/cli/blob/master/LICENSE). ### Authors [Jakub Roztocil](https://roztocil.co) ([@jakubroztocil](https://twitter.com/jakubroztocil)) created HTTPie and [these fine people](https://github.com/httpie/cli/blob/master/AUTHORS.md) have contributed. - diff --git a/httpie/output/formatters/json.py b/httpie/output/formatters/json.py index bc6151e42f..a3a1aad9d6 100644 --- a/httpie/output/formatters/json.py +++ b/httpie/output/formatters/json.py @@ -10,6 +10,7 @@ def __init__(self, **kwargs): self.enabled = self.format_options['json']['format'] def format_body(self, body: str, mime: str) -> str: + mime = mime.lower() maybe_json = [ 'json', 'javascript', diff --git a/tests/test_json.py b/tests/test_json.py index e758ebe7f4..b52366cd3e 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -112,6 +112,23 @@ def test_duplicate_keys_support_from_response(): assert JSON_WITH_DUPES_FORMATTED_UNSORTED in r +@pytest.mark.parametrize('content_type', [ + 'APPLICATION/JSON', + 'Application/Problem+JSON; charset=utf-8', +]) +@responses.activate +def test_json_formatter_accepts_case_insensitive_media_types(content_type): + responses.add( + responses.GET, + DUMMY_URL, + body='{"message":"ok"}', + content_type=content_type, + ) + response = http('--pretty=format', '--body', DUMMY_URL) + + assert '{\n "message": "ok"\n}' in response + + def test_duplicate_keys_support_from_input_file(): """JSON file with duplicate keys should be handled correctly.""" args = (