Cookies carry session state between HTTP requests. You can manually create a Netscape-format cookie jar for curl from cookie values you already have. Cookie values can also be captured from Set-Cookie responses or sent as a literal cookie without a jar.
cURL reads cookie files in the legacy Netscape cookie-jar format. Each cookie uses one physical line with seven TAB-separated fields for domain, subdomain matching, path, secure transport, expiry time, name, and value, while lines that start with # are treated as comments except for the special #HttpOnly_ prefix.
cURL only sends a cookie when the domain, path, protocol, and expiry fields match the target URL. Because these files often hold live session identifiers, keep them private and remove them when the request flow is finished.
Steps to create a cookie jar file for curl:
- Create a file named cookies.txt in your text editor with the header and one TAB-separated row for each cookie.
# Netscape HTTP Cookie File # https://curl.se/docs/http-cookies.html httpbin.org FALSE / TRUE 0 session sid_48291 httpbin.org FALSE / TRUE 0 account acct_48291
cURL skips a row whose fields are separated by spaces, so the editor must keep real TAB characters. Field two is TRUE only when the cookie must match subdomains, field four is FALSE only for a plain HTTP target, and an expiry of 0 denotes a session cookie.
- Restrict the file to the current account.
$ chmod 600 cookies.txt
Cookie jars are plain text, so loose permissions expose reusable session data to other local users.
- Verify that the server receives both cookies from cookies.txt.
$ curl --cookie cookies.txt https://httpbin.org/cookies { "cookies": { "account": "acct_48291", "session": "sid_48291" } }A response that echoes both names confirms that the domain, path, secure flag, and expiry values match the target request.
The jar can be reused by later requests. Once it is no longer needed, you can delete cookies.txt to reduce accidental session reuse.