Curl Send Cookies Example

Curl ignores cookies unless told otherwise. One flag sends them, another stores what the server sets, and using both with the same file gives you a session that survives between commands.

Last updated . How this page is checked.

The command

curl -b "session=abc123; theme=dark" https://httpbin.org/cookies

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-b "name=value"Send these cookies. Separate several with ; .
-b fileRead cookies from a file and turn on cookie handling.
-c fileAfter the transfer, write all cookies to this file (the "jar").
-jDiscard session cookies when loading a jar, as a browser restart would.

A session across several requests

A login sets a cookie in its response; later requests must send it back. Give curl a jar: -c writes cookies after each request and -b with the same filename sends the stored ones on the next. With a jar loaded, curl applies the normal rules for domain, path, expiry and the Secure flag, so each cookie goes only where a browser would send it.

# log in, saving whatever cookies the server sets
curl -c jar.txt -d "user=ada" -d "pass=secret" https://example.com/login

# use and refresh the same session
curl -b jar.txt -c jar.txt https://example.com/account

How -b decides between a string and a file

If the argument to -b contains an =, curl treats it as cookie data and sends it as written, to whatever host the request goes to. Otherwise it is a filename. An empty string, -b "", is a useful third case: it enables the cookie engine with no starting cookies, so cookies set during a redirect chain are carried along it.

The jar is a plain-text file in the Netscape format: one cookie per line with tab-separated domain, subdomain flag, path, secure flag, expiry time, name and value. It is readable, editable, and it contains live session tokens, so treat it like a password file.

curl -L -b "" https://httpbin.org/cookies/set/token/xyz   # cookie survives the redirect
cat jar.txt

Common mistakes

  • -b "name=value" skips all cookie rules. The value is sent to any host, including redirect targets on other domains.
  • -c alone does not send anything. It only records; pair it with -b.
  • A cookie is not always enough. Forms protected against CSRF also want a token from the page, sent back in a field or header.

Related curl examples

More under Headers

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I send a cookie with curl?

Use -b "name=value", for example curl -b "session=abc123" URL. Separate multiple cookies with a semicolon and a space.

How do I save cookies with curl?

Use -c filename. Curl writes every cookie it knows about to that file when the transfer finishes.

How do I keep a session between curl requests?

Pass the same file to both flags: curl -b jar.txt -c jar.txt URL. Cookies are read before the request and saved after it.