support nested multipart, strict no-content parts CRLF and better part metadata resolution #380
+149
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Features and Fixes
Given a part that has "no content" (
None
) (as if it was returned on its own by an HTTP 204 No Content response),MultipartEncoder
will not incorrectly insert a second\r\n
anymore. A parser that strictly interprets the amount of\r\n
can distinguish a "no content" part from an "empty string" part, which can have different meanings depending on the embeddedContent-Type
(e.g.: anull
vs""
for JSON).Fixes requests_toolbelt.multipart.decoder.ImproperBodyPartContentException: content does not contain CR-LF-CR-LF #352.
Multipart decoder correctly understands a single
\r\n
used to separate the headers from the "no content" body. The resulting part returnsNone
rather than""
. A part still using CR-LF-CR-LF explicitly will enclose an empty string.Add a
content_type
parameter to__init__
allowing to override the defaultmultipart/form-data
that was hard-coded.Support nested multipart.
Using the
MultipartEncoder
asfile_pointer
in the inputfields
of anotherMultipartEncoder
will correctly nest the contents, with their respective boundaries. This can be used to form complex structures ofmultipart/mixed
,multipart/alternate
,multipart/related
commonly used when combining various attachment representations.When
headers
are provided for one of the parts, ensure that anyContent-Type
,Content-Location
orContent-Disposition
explicitly provided in them are not overridden by theurllib3.fields.make_multipart
step. This allows using other dispositions thanform-data
, such asattachment
orinline
as needed.