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
8 changes: 8 additions & 0 deletions httpie/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ def get_multipart_data_and_content_type(
content_type: str = None,
) -> Tuple['MultipartEncoder', str]:
from requests_toolbelt import MultipartEncoder
from .utils import parse_content_type_header

if not boundary and content_type:
# Honor a boundary specified via an explicit `Content-Type` header
# (e.g. `Content-Type:multipart/form-data; boundary=xoxo`) so that the
# body delimiter matches the boundary declared in the header.
_, ct_params = parse_content_type_header(content_type)
boundary = ct_params.get('boundary')

encoder = MultipartEncoder(
fields=data.items(),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ def test_multipart_custom_content_type_boundary_preserved(self, httpbin):
assert f'multipart/magic; boundary={boundary_in_header}' in r
assert r.count(boundary_in_body) == 3

def test_multipart_boundary_from_content_type_without_flag(self, httpbin):
# A boundary specified only via an explicit Content-Type header
# (no --boundary) must be used for the body delimiter too.
boundary = 'HTTPIE_FTW'
r = http(
'--print=HB',
'--check-status',
'--multipart',
httpbin + '/post',
f'Content-Type: multipart/form-data; boundary={boundary}',
'AAAA=AAA',
'BBB=BBB',
)
assert f'multipart/form-data; boundary={boundary}' in r
# 1 in the header + 3 body delimiters (two parts + closing). Without the
# fix the body uses a random boundary and this count is 1.
assert r.count(boundary) == 4

def test_multipart_chunked(self, httpbin_with_chunked_support):
r = http(
'--verbose',
Expand Down
Loading