Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.

1 change: 1 addition & 0 deletions httpie/output/formatters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 17 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
Loading