Skip to content
Myr

Clarify multipart documentation (#580)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

*Clarify multipart behvaiour

authored by Mattwmaster58 · committed by Tom Christie · parent 3f68601 · Browse files · 1 file+9 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+9 added−9 removed

  • other:+9 added−9 removed

1 file+9 added−9 removed

modified:docs/advanced.md+9 added-9 removed
345 unmodified lines
346## Multipart file encoding347348As mentioned in the [quickstart](/quickstart#sending-multipart-file-uploads)349multipart file encoding is available by passing a dictionary with the350name of the payloads as keys and a tuple of elements as values.350name of the payloads as keys and either tuple of elements or a file-like object or a string as values.351352```python353>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}354>>> r = await httpx.post("https://httpbin.org/post", files=files)
6 unmodified lines
361 ...362}363```364365More specifically, this tuple must have at least two elements and maximum of three:365More specifically, if a tuple is used as a value, it must have between 2 and 3 elements:366367- The first one is an optional file name which can be set to `None`.368- The second may be a file-like object or a string which will be automatically367- The first element is an optional file name which can be set to `None`.368- The second element may be a file-like object or a string which will be automatically369encoded in UTF-8.370- An optional third element can be included with the370- An optional third element can be used to specify the 371[MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_Types)372of the file being uploaded. If not specified HTTPX will attempt to guess the MIME type373based on the file name specified as the first element or the tuple, if that374is set to `None` or it cannot be inferred from it, HTTPX will default to375`applicaction/octet-stream`.372of the file being uploaded. If not specified HTTPX will attempt to guess the MIME type based373on the file name, with unknown file extensions defaulting to "application/octet-stream".374If the file name is explicitly set to `None` then HTTPX will not include a content-type375MIME header field.376377```python378>>> files = {'upload-file': (None, 'text content', 'text/plain')}379>>> r = await httpx.post("https://httpbin.org/post", files=files)
70 unmodified lines