简体   繁体   中英

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)

PS This is working fine without 'with' statement.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM