'Urllib3 session (persist settings between requests)

I wrote a Python script which makes several https requests one after the other by following redirections (its purpose is to login to AWS Cognito via a load balancer):

import requests
    
session = requests.session()
    
response = session.get(
    f'https://<initial_url>',
    allow_redirects=False,
    verify=False,
)

response = session.get(
    response.headers["Location"],
    allow_redirects=False,
    verify=False,
)

response = session.post(
    response.headers["Location"],
    allow_redirects=False,
    verify=False,
    data={
        "_csrf": session.cookies["XSRF-TOKEN"],
        "username": <user>,
        "password": <password>,
    },
)

This works as expected. I would like to use the urllib3 library instead of requests and I transformed the script as follows:

import urllib3

http = urllib3.PoolManager(cert_reqs="CERT_NONE")

response = session.request(
    "GET",
    f'https://<initial_url>',
    redirect=False,
    retries=False,
)

response = session.request(
    "GET",
    response.headers["Location"],
    redirect=False,
    retries=False,
)

csrf=<get the XSRF-TOKEN cookie from response.headers["Set-Cookie"]>

fields = {
        "_csrf": csrf,
        "username": <user>,
        "password": <password>,
    }
response = session.request(
    "POST",
    response.headers["Location"],
    redirect=False,
    retries=False,
    fields=fields,
)

The GET requests work and the redirects are as expected, but the POST does not (I get an error from Cognito). Based on the documentation

https://docs.python-requests.org/en/latest/user/quickstart/

and

https://urllib3.readthedocs.io/en/latest/user-guide.html

I understand that the equivalent of requests/data is urllib3/fields, as both are used to form-encode data

In the first version of the code, I created a new requests.session() object before the POST request and used it for that and I got the same error as in the urrlib3 case, which led me to believe that urllib3.PoolManager() does not provide a session like requests.session() and that each request is made separately, which makes the POST request fail. Does anyone know if there is a way to have a session in urrlib3 ? I could not find anything in the documentation



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source