Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError when using requests.post() in `with` statement

A/C python requests documentation, with statement can be used with requests for better speed.

with requests.get('http://httpbin.org/get', stream=True) as r: # Do things with the response here.

So why is this returning 'Attribute error'?

Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
with requests.post(url,headers=headers,data=data,stream=True) as post_res:
AttributeError: __exit__

code:

with requests.post(url,headers=headers,data=data,stream=True) as post_res:
    print(b'Name' in post_res.content)

P.S. This is working fine without 'with' statement.

like image 788
kappa101 Avatar asked Jul 09 '17 13:07

kappa101


1 Answers

AFAICS a context manager is documented only for GET requests, not POST. That does make sense since POST is not indempotent anyway.

like image 84
user2722968 Avatar answered Sep 22 '22 03:09

user2722968