In Python's requests
documentation, it suggests sending multipart form data like so:
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)
This question shows the same behavior. However, this question by the same author uses a with
statement:
with open('data','rb') as payload:
headers = {'content-type': 'application/x-www-form-urlencoded'}
r = requests.post('https://IP_ADDRESS/rest/rest/2', auth=('userid', 'password'),
data=payload, verify=False, headers=headers)
My intuition is that I should always use a with
statement when using files; do I need it in this case or not? To be extra clear, I am not looking for advice as to whether or not to use with
. I am specifically referring to the interaction with the requests
library, and if I can omit the with
statement as the docs imply.