Skip to content
Myr

docs: Use `with` to open files in multipart examples (#3478)

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

authored by Bazyli Cyran · committed by GitHub · parent c7c13f1 · Browse files · 2 files+18 added−11 removed
This commit

State

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

Diff stats

2 files+18 added−11 removed

  • other:+18 added−11 removed

2 files+18 added−11 removed

modified:docs/advanced/clients.md+9 added-5 removed
268 unmodified lines
269multipart file encoding is available by passing a dictionary with the270name of the payloads as keys and either tuple of elements or a file-like object or a string as values.271272```pycon273>>> with open('report.xls', 'rb') as report_file:273>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}274>>> r = httpx.post("https://httpbin.org/post", files=files)274... files = {'upload-file': ('report.xls', report_file, 'application/vnd.ms-excel')}275... r = httpx.post("https://httpbin.org/post", files=files)276>>> print(r.text)277{278 ...279 "files": {
38 unmodified lines
318To do that, pass a list of `(field, <file>)` items instead of a dictionary, allowing you to pass multiple items with the same `field`.319For instance this request sends 2 files, `foo.png` and `bar.png` in one request on the `images` form field:320321```pycon321>>> files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),322 ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]323>>> r = httpx.post("https://httpbin.org/post", files=files)322>>> with open('foo.png', 'rb') as foo_file, open('bar.png', 'rb') as bar_file:323... files = [324... ('images', ('foo.png', foo_file, 'image/png')),325... ('images', ('bar.png', bar_file, 'image/png')),326... ]327... r = httpx.post("https://httpbin.org/post", files=files)328```
modified:docs/quickstart.md+9 added-6 removed
172 unmodified lines
173174You can also upload files, using HTTP multipart encoding:175176```pycon177>>> files = {'upload-file': open('report.xls', 'rb')}178>>> r = httpx.post("https://httpbin.org/post", files=files)177>>> with open('report.xls', 'rb') as report_file:178... files = {'upload-file': report_file}179... r = httpx.post("https://httpbin.org/post", files=files)180>>> print(r.text)181{182 ...183 "files": {
6 unmodified lines
190You can also explicitly set the filename and content type, by using a tuple191of items for the file value:192193```pycon194>>> with open('report.xls', 'rb') report_file:193>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}194>>> r = httpx.post("https://httpbin.org/post", files=files)195... files = {'upload-file': ('report.xls', report_file, 'application/vnd.ms-excel')}196... r = httpx.post("https://httpbin.org/post", files=files)197>>> print(r.text)198{199 ...200 "files": {
6 unmodified lines
207If you need to include non-file data fields in the multipart form, use the `data=...` parameter:208209```pycon210>>> data = {'message': 'Hello, world!'}209>>> files = {'file': open('report.xls', 'rb')}210>>> r = httpx.post("https://httpbin.org/post", data=data, files=files)211>>> with open('report.xls', 'rb') as report_file:212... files = {'file': report_file}213... r = httpx.post("https://httpbin.org/post", data=data, files=files)214>>> print(r.text)215{216 ...217 "files": {
330 unmodified lines