Search code examples
pythonpython-3.xsessionpython-requestssession-cookies

Why I'm not able to get session cookies?


import requests

with requests.session() as c:
    res = c.get('https://onlineedlreg.dotm.gov.np/dlNewRegHome', )
login_data ={'citizenshipID': '269',
             'statusType': 'NEWLICENSE'}
response = c.post("https://onlineedlreg.dotm.gov.np/dlNewRegHome",
                  data=login_data,)
r = c.get("https://onlineedlreg.dotm.gov.np/newDlApplicationEntry_.action")
print(res.cookies)
print(response.cookies)
print(r.cookies)

I think I'm supposed to get some cookies for response and r variable but I'm not getting so. what could be the problem. Code give following output:

<RequestsCookieJar[<Cookie JSESSIONID=B70AD6C7E6780768127766637046E760 for onlineedlreg.dotm.gov.np/>]>
<RequestsCookieJar[]>
<RequestsCookieJar[]>

Solution

  • Response.cookies only includes the cookies sent in the response. If you want the session cookies, use Session.cookies:

    with requests.session() as c:
        res = c.get('https://onlineedlreg.dotm.gov.np/dlNewRegHome', )
        login_data ={'citizenshipID': '269',
                     'statusType': 'NEWLICENSE'}
        response = c.post("https://onlineedlreg.dotm.gov.np/dlNewRegHome",
                          data=login_data,)
        print(c.cookies)