@@ -174,8 +174,9 @@ Form encoded data can also include multiple values from a given key.
174
174
You can also upload files, using HTTP multipart encoding:
175
175
176
176
``` 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)
179
180
>>> print (r.text)
180
181
{
181
182
...
@@ -190,8 +191,9 @@ You can also explicitly set the filename and content type, by using a tuple
190
191
of items for the file value:
191
192
192
193
``` 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)
195
197
>>> print (r.text)
196
198
{
197
199
...
@@ -206,8 +208,9 @@ If you need to include non-file data fields in the multipart form, use the `data
206
208
207
209
``` pycon
208
210
>>> 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)
211
214
>>> print (r.text)
212
215
{
213
216
...
0 commit comments