How do I send a cookie?
curl -b 'session=7f3a9c21; theme=dark' https://app.example.net/dashboard
-b, long form --cookie, accepts name and value pairs separated by semicolons. It is the quickest way to reproduce a request a browser made: copy the cookie value from the browser's developer tools and replay the request from your server.
How do I save cookies the server sets?
curl -c jar.txt https://app.example.net/ curl -b jar.txt -c jar.txt https://app.example.net/account
-c writes every cookie the server sets into a file, called a cookie jar, when curl exits. Passing the same file to -b reads it back. Using both on every request keeps the jar current as the server updates or adds cookies, exactly like a browser.
How do I script a login?
Most web logins follow the same pattern: fetch the form to get a session and a CSRF token, post the credentials, then use the session:
J=$(mktemp)
token=$(curl -s -c "$J" https://app.example.net/login | grep -oP 'name="_token" value="\K[^"]+')
curl -s -b "$J" -c "$J" --data-urlencode "_token=$token" \
--data-urlencode "email=ops@example.net" --data-urlencode "password=$APP_PASS" \
-o /dev/null -w '%{http_code}\n' https://app.example.net/login
curl -s -b "$J" https://app.example.net/reports/daily.csv -o daily.csv
rm -f "$J"
A 302 after the login post usually means success, redirecting to the dashboard. For sites with an API, prefer tokens over scripted logins; they are more stable and easier to revoke. Posting forms is covered in Send POST requests with curl.
What does the cookie jar look like?
curl uses the Netscape format: one cookie per line, seven tab-separated fields.
| Field | Example | Meaning |
|---|---|---|
| Domain | app.example.net | Host the cookie belongs to |
| Include subdomains | FALSE | Whether subdomains receive it |
| Path | / | URL path scope |
| Secure | TRUE | HTTPS only |
| Expires | 1796000000 | Unix time; 0 means session cookie |
| Name | session | Cookie name |
| Value | 7f3a9c21 | Cookie value |
Lines starting with #HttpOnly_ mark cookies the server flagged as HttpOnly. The jar contains live session secrets, so create it with mode 600 and delete it when done.
How do I start without old session cookies?
-j, long form --junk-session-cookies, ignores session cookies (those with expiry 0) when loading a jar, simulating a browser restart. Use it to test "remember me" behavior: persistent cookies survive, session cookies do not.
How do I debug Set-Cookie headers?
curl -sS -D - -o /dev/null https://app.example.net/login | grep -i '^set-cookie'
Check the attributes on each cookie your own site sets:
Secure, so it never travels over plain HTTP.HttpOnly, so page scripts cannot read session cookies.SameSite=LaxorStrict, which limits cross-site sending.- A sensible
Max-AgeorExpires; very long session lifetimes widen the window for stolen cookies.
When a login works in the browser but not in curl, the usual cause is a missing CSRF token, a cookie scoped to another path or subdomain, or a redirect to a different host that does not receive the cookie. -v shows which cookies curl sends on each request. Redirect handling is explained in Follow redirects with curl.
Can I keep a session in a long-running script?
Yes. Keep the jar in a private folder, reuse it across runs, and re-login when a request returns 401 or redirects to the login page. For monitoring logged-in pages from another city, a small machine such as E1 running these checks from a timer works well and keeps the check independent of the server it watches.
Can I edit cookies in the jar by hand?
Yes, carefully. The jar is plain text, so you can delete a line to drop one cookie or change a value for a test. Keep the seven tab-separated fields intact; spaces instead of tabs make curl ignore the line. For quick experiments, sending a single cookie with -b is safer than editing the file.
Keep Reading
Send POST requests with curl
POST forms, JSON, files and raw bodies with curl, choose the right content type and read the response status for scripts.
OpenFollow redirects with curl
Make curl follow 301, 302, 307 and 308 redirects with -L, limit hops, keep POST bodies, see the final URL and debug redirect loops.
OpenHeaders with curl
Show response headers, send or remove request headers, pick single header values and test caching, compression and virtual hosts with curl.
OpenThe curl command
A practical tour of curl for server admins: fetching, saving, timing, debugging, retrying, resolving and the flags that make scripts reliable.
OpenFAQ
Quick Answers
Use -b 'name=value', with semicolons between several cookies.
Use -c with a file name; curl writes all received cookies there when it exits.
Pass the same file to both -b and -c on every request.
The Netscape format: seven tab-separated fields per cookie.
Add -j to drop session cookies when loading the jar.