Ephemeral vs. Session mode
By default, eachfetch() call runs in ephemeral mode. This is a good fit for one-off requests.
For multi-step flows (like login sequences), use sessions to persist state.
| Scenario | Recommended | Why |
|---|---|---|
| One-off request, no cookie carryover | Ephemeral (default) | Request context is isolated per call |
| Multi-step login or reuse cookies | Session | Shared session context |
| Parallel jobs that must stay isolated | Ephemeral or per-job session | Avoid cross-talk between tasks |
Creating a session
Auto-disposing sessions
UsewithSession() to automatically close the session when done:
Session options
Sessions acceptbrowser, os, proxy, timeout, insecure, and defaultHeaders:
Per-request overrides
Within a session,browser, os, and proxy are fixed at creation time unless you pass an explicit transport for that request.
You can still override per-request values like timeout, headers, redirect, and body:
Cookie management
Sessions automatically handle cookies across requests, but you can also read and write cookies directly.Reading cookies
UsegetCookies(url) to inspect which cookies would be sent to a URL:
Setting cookies manually
UsesetCookie(name, value, url) to inject a cookie into the session jar:
Clearing cookies
UseclearCookies() to remove all cookies from the session:
Session isolation
Each session maintains its own:- Cookie jar: cookies are not shared between sessions
- Session identifier and defaults
Best practices
Always close sessions
Call
session.close() or use withSession() to prevent resource leaks.One session per flow
Use a dedicated session for each logical user flow or task.
Parallel isolation
For parallel scraping, create separate sessions to avoid cookie cross-contamination.
Reuse for workflow consistency
Sessions keep one context for multi-step flows.