Skip to content

Commit 10b7295

Browse files
authored
docs: Use with to open files in multipart examples (#3478)
1 parent c7c13f1 commit 10b7295

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

docs/advanced/clients.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ multipart file encoding is available by passing a dictionary with the
270270
name of the payloads as keys and either tuple of elements or a file-like object or a string as values.
271271

272272
```pycon
273-
>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}
274-
>>> r = httpx.post("https://httpbin.org/post", files=files)
273+
>>> with open('report.xls', 'rb') as report_file:
274+
... files = {'upload-file': ('report.xls', report_file, 'application/vnd.ms-excel')}
275+
... r = httpx.post("https://httpbin.org/post", files=files)
275276
>>> print(r.text)
276277
{
277278
...
@@ -318,7 +319,10 @@ To do that, pass a list of `(field, <file>)` items instead of a dictionary, allo
318319
For instance this request sends 2 files, `foo.png` and `bar.png` in one request on the `images` form field:
319320

320321
```pycon
321-
>>> 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)
324328
```

docs/quickstart.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ Form encoded data can also include multiple values from a given key.
174174
You can also upload files, using HTTP multipart encoding:
175175

176176
```pycon
177-
>>> 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)
179180
>>> print(r.text)
180181
{
181182
...
@@ -190,8 +191,9 @@ You can also explicitly set the filename and content type, by using a tuple
190191
of items for the file value:
191192

192193
```pycon
193-
>>> files = {'upload-file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel')}
194-
>>> r = httpx.post("https://httpbin.org/post", files=files)
194+
>>> with open('report.xls', 'rb') report_file:
195+
... files = {'upload-file': ('report.xls', report_file, 'application/vnd.ms-excel')}
196+
... r = httpx.post("https://httpbin.org/post", files=files)
195197
>>> print(r.text)
196198
{
197199
...
@@ -206,8 +208,9 @@ If you need to include non-file data fields in the multipart form, use the `data
206208

207209
```pycon
208210
>>> 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)
211214
>>> print(r.text)
212215
{
213216
...

0 commit comments

Comments
 (0)