@@ -174,8 +174,9 @@ Form encoded data can also include multiple values from a given key.
174174You 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
190191of 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