Reuse a token and cookies with requests.Session
QuizMediumPython onlyAsked in: Capgemini
Which code logs in once and makes every later request send the token and any cookies the server set?
- s = requests.Session(); token = s.post(url_login, json=creds, timeout=10).json()["token"]; s.headers.update({"Authorization": f"Bearer {token}"}); then s.get(...) for later calls
- requests.get(url, token=token)
- Call requests.post for login before each test; requests remembers it
- Put the token in params=
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.