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

Issue341 #342

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ htmlcov/
.tox/
venv/
venv*/
.venv/
build/
*.egg
.env
.cache/
.vscode
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Full Documentation
dumputils
formdata
exceptions
receiving-data
sessions
threading
uploading-data
Expand Down
13 changes: 13 additions & 0 deletions docs/receiving-data.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. _receiving-and-handling-data:

Receiving and Handling Multipart Data
=====================================

If you need, on occasion, to build a service to receive and handle ``multipart/form-data``,
such as an endpoint to receive data from an HTML form submission, the requests_toolbelt
package offers the capability of decoding the inbound :class:`bytes`.
This capability is provided by the :class:`requests_toolbelt.multipart.MultipartDecoder`

MultipartDecoder
----------------
.. autoclass:: requests_toolbelt.multipart.decoder.MultipartDecoder
17 changes: 12 additions & 5 deletions requests_toolbelt/multipart/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,28 @@ class MultipartDecoder(object):
The ``MultipartDecoder`` object parses the multipart payload of
a bytestring into a tuple of ``Response``-like ``BodyPart`` objects.

:param content: The bytestring to parse
:type content: bytes
:param content_type: The value of the Content-Type header
:type content_type: str
:param encoding: The encoding of the response body, defaults to ``'utf-8'``
:type encoding: str

The basic usage is::

import requests
from requests_toolbelt import MultipartDecoder

response = requests.get(url)
decoder = MultipartDecoder.from_response(response)
decoder = MultipartDecoder(content, content_type)
for part in decoder.parts:
print(part.headers['content-type'])

If the multipart content is not from a response, basic usage is::
If the multipart content is from a response, basic usage is::

import requests
from requests_toolbelt import MultipartDecoder

decoder = MultipartDecoder(content, content_type)
response = requests.get(url)
decoder = MultipartDecoder.from_response(response)
for part in decoder.parts:
print(part.headers['content-type'])

Expand Down