Skip to content
Closed
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: 1 addition & 2 deletions httpie/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading