From 0aeca2045045a85a3697bb297de48971ad4f8b77 Mon Sep 17 00:00:00 2001 From: Evan King Date: Wed, 22 Aug 2018 11:58:16 -0700 Subject: [PATCH 1/3] Use a with statement when posting files The original documentation implies that `requests` will close the file for you; this is not the case. Since it is up to the user to release the resources, it should be reflected as such in the docs. --- docs/user/quickstart.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 699c0ffd9a..ae23843f84 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -298,9 +298,9 @@ Requests makes it simple to upload Multipart-encoded files:: >>> url = 'http://httpbin.org/post' >>> files = {'file': open('report.xls', 'rb')} - - >>> r = requests.post(url, files=files) - >>> r.text + >>> with open('report.xls', 'rb') as f: + r = requests.post(url, files={'file': f}) + r.text { ... "files": { From aca12116c28b3b228a6e7396ff2c923723dba5fc Mon Sep 17 00:00:00 2001 From: Evan King Date: Wed, 22 Aug 2018 11:59:16 -0700 Subject: [PATCH 2/3] typo fix --- docs/user/quickstart.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index ae23843f84..8dd60dc8a3 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -297,7 +297,6 @@ POST a Multipart-Encoded File Requests makes it simple to upload Multipart-encoded files:: >>> url = 'http://httpbin.org/post' - >>> files = {'file': open('report.xls', 'rb')} >>> with open('report.xls', 'rb') as f: r = requests.post(url, files={'file': f}) r.text From 4245d215477f606b37dd9d45790d8a704c4995a3 Mon Sep 17 00:00:00 2001 From: Evan King Date: Wed, 22 Aug 2018 12:02:34 -0700 Subject: [PATCH 3/3] changed the second example as well --- docs/user/quickstart.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 8dd60dc8a3..4da5667276 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -311,10 +311,9 @@ Requests makes it simple to upload Multipart-encoded files:: You can set the filename, content_type and headers explicitly:: >>> url = 'http://httpbin.org/post' - >>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})} - - >>> r = requests.post(url, files=files) - >>> r.text + >>> with open('report.xls', 'rb') as f: + r = requests.post(url, files={'file': ('report.xls', f, 'application/vnd.ms-excel', {'Expires': '0'})}) + r.text { ... "files": {