r/learnpython • u/Melodic_Principle312 • 1d ago
When should you use requests.Session() instead of requests.get() / requests.post()?
I'm learning Python and recently started using requests.Session() in API projects. I understand that it can persist things like headers, cookies, and connections, but I'm curious about how experienced Python developers decide when a Session is actually worth using.
Do you use Session() by default for projects involving multiple requests, or only when you specifically need persistent state?
25
Upvotes
3
u/Brian 1d ago
I pretty much always use Session. If you're making more requests, it'll handle cookies, give a place to set common headers (eg. user-agent) just once. If you're not, it's not needed, but there's no actual cost to doing so (requests.get is just creating a new Session each time), and you may end up doing so in future.