CINXE.COM
curl - HTTP Cookies
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>curl - HTTP Cookies</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <link rel="stylesheet" type="text/css" href="/curl.css"> <link rel="shortcut icon" href="/favicon.ico"> <link rel="icon" href="/logo/curl-symbol.svg" type="image/svg+xml"> <link rel="alternate" type="application/rss+xml" title="cURL Releases" href="https://github.com/curl/curl/releases.atom"> </head> <body> <div class="main"> <div class="menu"> <a href="/docs/" class="menuitem" title="Documentation Overview">Docs Overview</a> <div class="dropdown"> <a class="dropbtn" href="/docs/projdocs.html">Project</a> <div class="dropdown-content"> <a href="/docs/bugbounty.html">Bug Bounty</a> <a href="/docs/bugs.html">Bug Report</a> <a href="/docs/code-of-conduct.html">Code of conduct</a> <a href="/docs/libs.html">Dependencies</a> <a href="/donation.html">Donate</a> <a href="/docs/faq.html">FAQ</a> <a href="/docs/features.html">Features</a> <a href="/docs/governance.html">Governance</a> <a href="/docs/history.html">History</a> <a href="/docs/install.html">Install</a> <a href="/docs/knownbugs.html">Known Bugs</a> <a href="/logo/">Logo</a> <a href="/docs/todo.html">TODO</a> <a href="/about.html">website Info</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/protdocs.html">Protocols</a> <div class="dropdown-content"> <a href="/docs/caextract.html">CA Extract</a> <a href="/docs/http-cookies.html">HTTP cookies</a> <a href="/docs/http3.html">HTTP/3</a> <a href="/docs/mqtt.html">MQTT</a> <a href="/docs/sslcerts.html">SSL certs</a> <a href="/docs/ssl-compared.html">SSL libs compared</a> <a href="/docs/url-syntax.html">URL syntax</a> <a href="/docs/websocket.html">WebSocket</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/reldocs.html">Releases</a> <div class="dropdown-content"> <a href="/ch/">Changelog</a> <a href="/docs/security.html">curl CVEs</a> <a href="/docs/releases.html">Release Table</a> <a href="/docs/versions.html">Version Numbering</a> <a href="/docs/vulnerabilities.html">Vulnerabilities</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/tooldocs.html">Tool</a> <div class="dropdown-content"> <a href="/docs/comparison-table.html">Comparison Table</a> <a href="/docs/manpage.html">curl man page</a> <a href="/docs/httpscripting.html">HTTP Scripting</a> <a href="/docs/mk-ca-bundle.html">mk-ca-bundle</a> <a href="/docs/tutorial.html">Tutorial</a> <a href="optionswhen.html">When options were added</a> </div> </div> <div class="dropdown"> <a class="dropbtn" href="/docs/whodocs.html">Who and Why</a> <div class="dropdown-content"> <a href="/docs/companies.html">Companies</a> <a href="/docs/copyright.html">Copyright</a> <a href="/sponsors.html">Sponsors</a> <a href="/docs/thanks.html">Thanks</a> <a href="/docs/thename.html">The name</a> </div> </div> </div> <div class="contents"> <div class="where"><a href="/">curl</a> / <a href="/docs/">Docs</a> / <a href="/docs/protdocs.html">Protocols</a> / <b>HTTP Cookies</b></div> <div class="relatedbox"> <b>Related:</b> <br><a href="manpage.html">curl man page</a> <br><a href="httpscripting.html">HTTP Scripting</a> <br><a href="tutorial.html">Tutorial</a> </div> <!-- Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. SPDX-License-Identifier: curl --> <h1 id="http-cookies">HTTP Cookies</h1> <h2 id="cookie-overview">Cookie overview</h2> <p>Cookies are <code>name=contents</code> pairs that an HTTP server tells the client to hold and then the client sends back those to the server on subsequent requests to the same domains and paths for which the cookies were set.</p> <p>Cookies are either "session cookies" which typically are forgotten when the session is over which is often translated to equal when browser quits, or the cookies are not session cookies they have expiration dates after which the client throws them away.</p> <p>Cookies are set to the client with the Set-Cookie: header and are sent to servers with the Cookie: header.</p> <p>For a long time, the only spec explaining how to use cookies was the original <a href="https://curl.se/rfc/cookie_spec.html">Netscape spec from 1994</a>.</p> <p>In 2011, <a href="https://www.ietf.org/rfc/rfc6265.txt">RFC 6265</a> was finally published and details how cookies work within HTTP. In 2016, an update which added support for prefixes was <a href="https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00">proposed</a>, and in 2017, another update was <a href="https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01">drafted</a> to deprecate modification of 'secure' cookies from non-secure origins. Both of these drafts have been incorporated into a proposal to <a href="https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-11">replace</a> RFC 6265. Cookie prefixes and secure cookie modification protection has been implemented by curl.</p> <p>curl considers <code>http://localhost</code> to be a <em>secure context</em>, meaning that it allows and uses cookies marked with the <code>secure</code> keyword even when done over plain HTTP for this host. curl does this to match how popular browsers work with secure cookies.</p> <h2 id="super-cookies">Super cookies</h2> <p>A single cookie can be set for a domain that matches multiple hosts. Like if set for <code>example.com</code> it gets sent to both <code>aa.example.com</code> as well as <code>bb.example.com</code>.</p> <p>A challenge with this concept is that there are certain domains for which cookies should not be allowed at all, because they are <em>Public Suffixes</em>. Similarly, a client never accepts cookies set directly for the top-level domain like for example <code>.com</code>. Cookies set for <em>too broad</em> domains are generally referred to as <em>super cookies</em>.</p> <p>If curl is built with PSL (<strong>Public Suffix List</strong>) support, it detects and discards cookies that are specified for such suffix domains that should not be allowed to have cookies.</p> <p>if curl is <em>not</em> built with PSL support, it has no ability to stop super cookies.</p> <h2 id="cookies-saved-to-disk">Cookies saved to disk</h2> <p>Netscape once created a file format for storing cookies on disk so that they would survive browser restarts. curl adopted that file format to allow sharing the cookies with browsers, only to see browsers move away from that format. Modern browsers no longer use it, while curl still does.</p> <p>The Netscape cookie file format stores one cookie per physical line in the file with a bunch of associated meta data, each field separated with TAB. That file is called the cookie jar in curl terminology.</p> <p>When libcurl saves a cookie jar, it creates a file header of its own in which there is a URL mention that links to the web version of this document.</p> <h2 id="cookie-file-format">Cookie file format</h2> <p>The cookie file format is text based and stores one cookie per line. Lines that start with <code>#</code> are treated as comments. An exception is lines that start with <code>#HttpOnly_</code>, which is a prefix for cookies that have the <code>HttpOnly</code> attribute set.</p> <p>Each line that specifies a single cookie consists of seven text fields separated with TAB characters. A valid line must end with a newline character.</p> <h3 id="fields-in-the-file">Fields in the file</h3> <p>Field number, what type and example data and the meaning of it:</p> <ol start="0"> <li>string <code>example.com</code> - the domain name</li> <li>boolean <code>FALSE</code> - include subdomains</li> <li>string <code>/foobar/</code> - path</li> <li>boolean <code>TRUE</code> - send/receive over HTTPS only</li> <li>number <code>1462299217</code> - expires at - seconds since Jan 1st 1970, or 0</li> <li>string <code>person</code> - name of the cookie</li> <li>string <code>daniel</code> - value of the cookie</li> </ol> <h2 id="cookies-with-curl-the-command-line-tool">Cookies with curl the command line tool</h2> <p>curl has a full cookie "engine" built in. If you just activate it, you can have curl receive and send cookies exactly as mandated in the specs.</p> <p>Command line options:</p> <p><code>-b, --cookie</code></p> <p>tell curl a file to read cookies from and start the cookie engine, or if it is not a file it passes on the given string. <code>-b name=var</code> works and so does <code>-b cookiefile</code>.</p> <p><code>-j, --junk-session-cookies</code></p> <p>when used in combination with -b, it skips all "session cookies" on load so as to appear to start a new cookie session.</p> <p><code>-c, --cookie-jar</code></p> <p>tell curl to start the cookie engine and write cookies to the given file after the request(s)</p> <h2 id="cookies-with-libcurl">Cookies with libcurl</h2> <p>libcurl offers several ways to enable and interface the cookie engine. These options are the ones provided by the native API. libcurl bindings may offer access to them using other means.</p> <p><code>CURLOPT_COOKIE</code></p> <p>Is used when you want to specify the exact contents of a cookie header to send to the server.</p> <p><code>CURLOPT_COOKIEFILE</code></p> <p>Tell libcurl to activate the cookie engine, and to read the initial set of cookies from the given file. Read-only.</p> <p><code>CURLOPT_COOKIEJAR</code></p> <p>Tell libcurl to activate the cookie engine, and when the easy handle is closed save all known cookies to the given cookie jar file. Write-only.</p> <p><code>CURLOPT_COOKIELIST</code></p> <p>Provide detailed information about a single cookie to add to the internal storage of cookies. Pass in the cookie as an HTTP header with all the details set, or pass in a line from a Netscape cookie file. This option can also be used to flush the cookies etc.</p> <p><code>CURLOPT_COOKIESESSION</code></p> <p>Tell libcurl to ignore all cookies it is about to load that are session cookies.</p> <p><code>CURLINFO_COOKIELIST</code></p> <p>Extract cookie information from the internal cookie storage as a linked list.</p> <h2 id="cookies-with-javascript">Cookies with JavaScript</h2> <p>These days a lot of the web is built up by JavaScript. The web browser loads complete programs that render the page you see. These JavaScript programs can also set and access cookies.</p> <p>Since curl and libcurl are plain HTTP clients without any knowledge of or capability to handle JavaScript, such cookies are not detected or used.</p> <p>Often, if you want to mimic what a browser does on such websites, you can record web browser HTTP traffic when using such a site and then repeat the cookie operations using curl or libcurl.</p> </div> </div> </body> </html>