Skip to content
Myr

Add support for multiple files per POST field (#1032)

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

* Changed RequestFiles type * Changed RequestFiles type 2 * Added test for multiple files same field * Lint * Mypy no idea * Added doc * Fixed some docs typos * Checking the right instance type and deleting the mypy ignore * Docs clarification * Back on images form field, with other files modified

authored by euri10 · committed by GitHub · parent 0f7d644 · Browse files · 4 files+60 added−2 removed
This commit

State

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

Diff stats

4 files+60 added−2 removed

  • tests:+47 added−0 removed
  • other:+13 added−2 removed

4 files+60 added−2 removed

modified:docs/advanced.md+10 added
462 unmodified lines
463!!! tip464 It is safe to upload large files this way. File uploads are streaming by default, meaning that only one chunk will be loaded into memory at a time.465 466 Non-file data fields can be included in the multipart form using by passing them to `data=...`.467 468You can also send multiple files in one go with a multiple file field form.469To do that, pass a list of `(field, <file>)` items instead of a dictionary, allowing you to pass multiple items with the same `field`.470For instance this request sends 2 files, `foo.png` and `bar.png` in one request on the `images` form field: 471472```python473>>> files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),474 ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]475>>> r = httpx.post("https://httpbin.org/post", files=files)476```477478## Customizing authentication479480When issuing requests or instantiating a client, the `auth` argument can be used to pass an authentication scheme to use. The `auth` argument may be one of the following...481
211 unmodified lines
modified:httpx/_content_streams.py+2 added-1 removed
326 unmodified lines
327 yield self.DataField(name=name, value=item)328 else:329 yield self.DataField(name=name, value=value)330331 file_items = files.items() if isinstance(files, typing.Mapping) else files331 for name, value in files.items():332 for name, value in file_items:333 yield self.FileField(name=name, value=value)334335 def iter_chunks(self) -> typing.Iterator[bytes]:336 for field in self.fields:
68 unmodified lines
modified:httpx/_types.py+1 added-1 removed
70 unmodified lines
71 Tuple[Optional[str], FileContent],72 # (filename, file (or text), content_type)73 Tuple[Optional[str], FileContent, Optional[str]],74]75RequestFiles = Mapping[str, FileTypes]75RequestFiles = Union[Mapping[str, FileTypes], List[Tuple[str, FileTypes]]]
modified:tests/test_content_streams.py+47 added
202 unmodified lines
203204def test_invalid_argument():205 with pytest.raises(TypeError):206 encode(123)207208209@pytest.mark.asyncio210async def test_multipart_multiple_files_single_input_content():211 files = [212 ("file", io.BytesIO(b"<file content 1>")),213 ("file", io.BytesIO(b"<file content 2>")),214 ]215 stream = encode(files=files, boundary=b"+++")216 sync_content = b"".join([part for part in stream])217 async_content = b"".join([part async for part in stream])218219 assert stream.can_replay()220 assert stream.get_headers() == {221 "Content-Length": "271",222 "Content-Type": "multipart/form-data; boundary=+++",223 }224 assert sync_content == b"".join(225 [226 b"--+++\r\n",227 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',228 b"Content-Type: application/octet-stream\r\n",229 b"\r\n",230 b"<file content 1>\r\n",231 b"--+++\r\n",232 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',233 b"Content-Type: application/octet-stream\r\n",234 b"\r\n",235 b"<file content 2>\r\n",236 b"--+++--\r\n",237 ]238 )239 assert async_content == b"".join(240 [241 b"--+++\r\n",242 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',243 b"Content-Type: application/octet-stream\r\n",244 b"\r\n",245 b"<file content 1>\r\n",246 b"--+++\r\n",247 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',248 b"Content-Type: application/octet-stream\r\n",249 b"\r\n",250 b"<file content 2>\r\n",251 b"--+++--\r\n",252 ]253 )