no persistence with cookies and headers with requests.session

16 Views Asked by At

I have an issue using requests package from Python, I'm sending two requests, one POST and one GET using the same URL domain. I realized that my headers and cookies are not persistent.

Basically, I'm doing it that way:

cookies = get_very_long_and_complex_cookies()
response = session.post(url,
                        headers = headers,
                        cookies = cookies,
                        files   = multipart_form_data)
[...]
response = session.get(url)

The first request is fine but the second one, do not contain the cookies and headers set for the first request. How can I achieve this ?

Thanks

EDIT: I'm answering my own question for history.

So session object contain a cookies attribute. It can be used to saved the cookie inside a session.

cookies = get_it_from_somewhere()
for n, v in cookies.items():
    session.cookies.set(n, v)
0

There are 0 best solutions below