Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MultipartWriter.append* no longer returning part/payload. #2759

Merged
Merged
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
1 change: 1 addition & 0 deletions CHANGES/2759.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix MultipartWriter.append* no longer returning part/payload.
12 changes: 5 additions & 7 deletions aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
CONTENT_TRANSFER_ENCODING, CONTENT_TYPE)
from .helpers import CHAR, TOKEN, parse_mimetype, reify
from .http import HttpParser
from .payload import (BytesPayload, LookupError, Payload, StringPayload,
from .payload import (JsonPayload, LookupError, Payload, StringPayload,
get_payload, payload_type)


Expand Down Expand Up @@ -712,10 +712,10 @@ def append(self, obj, headers=None):
obj.headers.update(headers)
else:
obj._headers = headers
self.append_payload(obj)
return self.append_payload(obj)
else:
try:
self.append_payload(get_payload(obj, headers=headers))
return self.append_payload(get_payload(obj, headers=headers))
except LookupError:
raise TypeError

Expand Down Expand Up @@ -752,16 +752,14 @@ def append_payload(self, payload):
).encode('utf-8') + b'\r\n'

self._parts.append((payload, headers, encoding, te_encoding))
return payload

def append_json(self, obj, headers=None):
"""Helper to append JSON part."""
if headers is None:
headers = CIMultiDict()

data = json.dumps(obj).encode('utf-8')
self.append_payload(
BytesPayload(
data, headers=headers, content_type='application/json'))
return self.append_payload(JsonPayload(obj, headers=headers))

def append_form(self, obj, headers=None):
"""Helper to append form urlencoded part."""
Expand Down
4 changes: 2 additions & 2 deletions docs/multipart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ will include the file's basename::
part = root.append(open(__file__, 'rb'))

If you want to send a file with a different name, just handle the
:class:`BodyPartWriter` instance which :meth:`MultipartWriter.append` will
:class:`Payload` instance which :meth:`MultipartWriter.append` will
always return and set `Content-Disposition` explicitly by using
the :meth:`BodyPartWriter.set_content_disposition` helper::
the :meth:`Payload.set_content_disposition` helper::

part.set_content_disposition('attachment', filename='secret.txt')

Expand Down