diff --git a/docs/user/advanced.rst b/docs/user/advanced.rst index c664a83d30..fe434ae616 100644 --- a/docs/user/advanced.rst +++ b/docs/user/advanced.rst @@ -399,11 +399,12 @@ upload image files to an HTML form with a multiple file field 'images':: To do that, just set files to a list of tuples of ``(form_field_name, file_info)``:: >>> url = 'https://httpbin.org/post' - >>> multiple_files = [ - ... ('images', ('foo.png', open('foo.png', 'rb'), 'image/png')), - ... ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))] - >>> r = requests.post(url, files=multiple_files) - >>> r.text + >>> with open('foo.png', 'rb') as fd1, open('bar.png', 'rb') as fd2: + ... multiple_files = [ + ... ('images', ('foo.png', fd1, 'image/png')), + ... ('images', ('bar.png', fd2, 'image/png'))] + ... r = requests.post(url, files=multiple_files) + ... r.text { ... 'files': {'images': 'data:image/png;base64,iVBORw ....'} diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 7fac5ce735..cce3934fde 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -303,10 +303,11 @@ POST a Multipart-Encoded File Requests makes it simple to upload Multipart-encoded files:: >>> url = 'https://httpbin.org/post' - >>> files = {'file': open('report.xls', 'rb')} + >>> with open('report.xls', 'rb') as fd: + ... files = {'file': fd} - >>> r = requests.post(url, files=files) - >>> r.text + ... r = requests.post(url, files=files) + ... r.text { ... "files": { @@ -318,10 +319,11 @@ Requests makes it simple to upload Multipart-encoded files:: You can set the filename, content_type and headers explicitly:: >>> url = 'https://httpbin.org/post' - >>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})} + >>> with open('report.xls', 'rb') as fd: + ... files = {'file': ('report.xls', fd, 'application/vnd.ms-excel', {'Expires': '0'})} - >>> r = requests.post(url, files=files) - >>> r.text + ... r = requests.post(url, files=files) + ... r.text { ... "files": {