From 8b1093245cf3e4009e2d50d28ac979272e492c75 Mon Sep 17 00:00:00 2001 From: "Terence D. Honles" Date: Sun, 25 Feb 2018 17:06:33 -0800 Subject: [PATCH 1/2] fix MultipartWriter.append* no longer returning part/payload - Fixes commit: caa6bdb2484bf821b52a65322efa98a889b593de MultipartWriter.append methods used to return the part appended to the writer so one could set the content_disposition, etc. This patch restores that functionality so the code matches the documentation in multipart.rst - This patch also makes append_json use the JsonPayload object instead of duplicating functionality. --- aiohttp/multipart.py | 12 +++++------- docs/multipart.rst | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/aiohttp/multipart.py b/aiohttp/multipart.py index 5e49715bcee..f28a0896ea8 100644 --- a/aiohttp/multipart.py +++ b/aiohttp/multipart.py @@ -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) @@ -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 @@ -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.""" diff --git a/docs/multipart.rst b/docs/multipart.rst index bebdb15159c..2c6cb40483d 100644 --- a/docs/multipart.rst +++ b/docs/multipart.rst @@ -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') From cad70ba50c688deb2714760070a310bdfb3b9e03 Mon Sep 17 00:00:00 2001 From: "Terence D. Honles" Date: Sun, 25 Feb 2018 17:29:35 -0800 Subject: [PATCH 2/2] add change entry --- CHANGES/2759.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/2759.bugfix diff --git a/CHANGES/2759.bugfix b/CHANGES/2759.bugfix new file mode 100644 index 00000000000..9deacfbc2ed --- /dev/null +++ b/CHANGES/2759.bugfix @@ -0,0 +1 @@ +Fix MultipartWriter.append* no longer returning part/payload.