Pulling server set cookies out in Python

KallDrexx

Ars Tribunus Militum
2,040
Can someone sanity check me? I've never used Python before and this is driving me mad.

I'm trying to write python to use locust.io for load testing an API workflow. To access the API you have to log in, which uses cookie based forms authentication in Asp.net MVC. However, despite a `Set-Cookie` header being returned python claims I have no cookies.

Code:
>>> import requests
>>> session = requests.session()
>>> response = session.post("http://localhost:46551/login", {'Email':'dev', 'Password':'1234'})
>>> print(response)
<Response [200]>
>>> print(response.cookies)
<RequestsCookieJar[]>
>>> response.headers
{'Cache-Control': 'no-cache', 'Pragma': 'no-cache', 'Content-Type': 'application/json; charset=utf-8', 'Expires': '-1', 'Server': 'Microsoft-IIS/10.0', 'UnAuthenticatedUser': 'true', 'X-AspNet-Version': '4.0.30319', 'Set-Cookie': 'Fuze=SOME-VALUE-HERE; domain=localhost; expires=Wed, 12-Sep-2018 15:05:22 GMT; path=/; HttpOnly, profile=29; domain=localhost; path=/; HttpOnly', 'X-SourceFiles': '=?UTF-8?B?QzpcY29kZVx3b3JrXGNhcmVzdGFja1xDYXJlU3RhY2suVW5pZmllZC5tc2hhcGlyb1xDYXJlU3RhY2suQmFja2VuZFxDYXJlU3RhY2suV2ViXGxvZ2lu?=', 'X-Powered-By': 'ASP.NET', 'Date': 'Wed, 12 Sep 2018 14:29:22 GMT', 'Content-Length': '42'}
>>> len(response.cookies)
0

I need that `Fuze` cookie to be set in the session so subsequent requests are valid. Am I doing something stupidly wrong here?
 
Can't really speak to why requests isn't liking what your server is sending to add to the cookie jar. In the good news category, it's sitting right in response headers. You should be able to build a dictionary and send it as part of a subsequent request. This will likely require some string parsing, but there's a bunch of helpers in requests.utils. See https://stackoverflow.com/questions/172 ... 6#17240616 for more info.