Python Request module not getting cookie from response -
i'm writing code trying cookies webserver using request module, confused me that:
- i'm seeing cookies returned when tested using postman - rest client
- i didn't sent cookies in sent request, surprised me find cookie want in sent requests.
i want cookie data use in code request application.
following code:
import requests import urllib3.contrib.pyopenssl urllib3.contrib.pyopenssl.inject_into_urllib3() username = 'user123' # real username/password not showing password = '1234567' login_data = {'id':username, 'pass_word':password, 'action': 'login'} r = requests.post("www.example.com/login/", data=login_data) print r.cookies print r.request.header['cookie']
output:
<<class 'requests.cookies.requestscookiejar'>[]> # why nothing?? {'accept-encoding': 'gzip, deflate', 'accept': '*/*', 'user-agent': 'python-requests/2.6.0 cpython/2.7.6 darwin/14.1.0', 'connection': 'keep-alive', 'cookie': 'blahblahblahblah', 'content-type': 'application/x-www-form-urlencoded'}
for confidential reason, cannot show real cookies data here, see it's in send request, how come? didn't tell send in request, , data expect response cookie, showed none.
the r.cookies
jar contains cookies new, server has sent in current response, cookies have been sent not appear there unless server resent them. sent cookies appear in r.request.headers
(the jar @ r.request._cookies
). if want preserve cookies across multiple requests, use session
:
session = requests.session() session.get(...) # first request session.get(...) # following requests pass cookies on
while using session, can retrieve cookies looking in session.cookies
. don't know why saw cookies sent when didn't, i'd have see more code.
Comments
Post a Comment