Terminus by Warp
Use Cookies With cURL

Use Cookies With cURL

Oscar Forero
Oscar Forero

Cookies are small pieces of data stored on a user's device by websites, containing information such as user preferences, session identifiers, or tracking data, which are sent back to the website with subsequent requests, allowing the website to recognize and customize the user's experience.

The short answer

To store the cookies contained in the response sent by the server in response to a request sent using the [.inline-code] curl[.inline-code]  command, you can use the [.inline-code] -c[.inline-code]  flag (short for [.inline-code] --cookie-jar[.inline-code] ) as follows:

 $ curl -c <file> <url>

Where:

  • [.inline-code] file[.inline-code]  is the relative or absolute path to the file you want to store the cookies in.
  • [.inline-code] url[.inline-code]  is the URL the request is sent to.

For example, the following command will send a request to the server located at [.inline-code] http://example.com[.inline-code]  and store the cookies contained in the response in a file called [.inline-code] cookies.txt[.inline-code]  in the local directory:

 $ curl -c cookies.txt http://example.com

Note that if the file does not exist, cURL will create it only if the server sends any cookies at all.

On the other hand, to send the cookies stored in a file alongside with a request with the [.inline-code] curl[.inline-code]  command, you can use the [.inline-code] -b[.inline-code]  flag as follows:

 $ curl -b <file> <url>

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code] curl with cookies[.inline-code]  in the AI Command Suggestions will prompt a list of [.inline-code] curl[.inline-code]  commands that can then quickly be inserted into your shell by doing [.inline-code] CMD+ENTER[.inline-code] .

[#store-temporary-cookies] Storing temporary cookies using an environment variable [#store-temporary-cookies]

Since cookies can contain sensitive information such as access tokens, it is sometimes preferable to temporarily keep them in-memory rather than saving them in a file.

This can be done through the use of an environment variable that will be automatically removed from the shell's environment once the session is closed.

To do so, you can use the following command:

 $ export <variable>=$(curl -c - --output /dev/null <url>)

Where:

  • [.inline-code] variable[.inline-code]  is the name of the environment variable the cookies will be stored in.
  • [.inline-code] -[.inline-code]  is used to write the cookies to the standard output.
  • [.inline-code] --output /dev/null[.inline-code]  is used to ignore the HTML content.

For example, the following command will store the received cookies in an environment variable named [.inline-code] cookies[.inline-code]  and discard the content of the response in the [.inline-code] /dev/null[.inline-code]  device:

 $ export cookies=$(curl -c - --output /dev/null http://example.com)

Using cookies stored in an environment variable

To then send the cookies stored in an environment variable alongside with a request, you can use the following syntax:

 $ echo $<variable> | curl -b - <url>

Where:

  • [.inline-code] variable[.inline-code]  is the name of the environment variable containing the cookies.
  • [.inline-code] -[.inline-code]  is used to read the cookies from the standard input.

For example, the following command will…:

 $ echo $cookies | curl -b - http://example.com

You can learn more about authentication and access tokens with our article on how to perform Basic Authentication with cURL.

[#send-a-single-synthetic-cookie] Sending synthetic cookies with cURL [#send-a-single-synthetic-cookie]

In some cases, the value of the cookies that need to be used might be known and constant, and can be directly hard-coded into the request sent with [.inline-code] curl[.inline-code] .

To send a single hard-coded cookie with [.inline-code] curl[.inline-code]  instead of using a file, you can use the [.inline-code] -b[.inline-code]  flag as follows:

 $ curl -b "<name>=<value>" <url>

Where:

  • [.inline-code] name[.inline-code]  is the name of the cookie.
  • [.inline-code] value[.inline-code]  is the value of the cookie.

For example, the following command will send alongside with the request a cookie named [.inline-code] token[.inline-code]  containing the value [.inline-code] ab3fox[.inline-code]  to the [.inline-code] http://example.com[.inline-code]  URL:

 $ curl -b "token=ab3fox" http://example.com

[#send-multiples-synthetic-cookies] Sending a request with multiple cookies [#send-multiples-synthetic-cookies]

To send multiple hard-coded cookies at once, you can either repeat the [.inline-code] -b[.inline-code]  flag multiple times as follows:

 $ curl -b "<name>=<value>" [-b "<name>=<value>" …] <url>

Or you can separate the list of key-value pairs using a semicolon as follows:

$ curl -b "<name>=<value>[;<name>=<value> …]" <url>

For example, the following command will send alongside with the request two cookies named [.inline-code] token[.inline-code]  and [.inline-code] access[.inline-code]  to the [.inline-code] http://example.com[.inline-code]  URL:

 $ curl -b "token=ab3fox" -b "access=admin" http://example.com

Which is equivalent to this command:

 $ curl -b "token=ab3fox;access=admin" http://example.com

[#removed-stored-cookies] Removing stored cookies [#removed-stored-cookies]

To remove the cookies stored in a file with [.inline-code] curl -c[.inline-code] , you can use the [.inline-code] rm[.inline-code]  command to permanently remove this file as follows:

 $ rm <file>

To remove the cookies stored in an environment variable, you can either close your terminal to end the session or use the [.inline-code] unset[.inline-code]  command as follows:

 $ unset <variable>

[#use-cookies-from-chrome] Using cookies stored in Chrome with cURL [#use-cookies-from-chrome]

There may be occasions in which we need to get the cookies and other headers sent by browsers like Chrome to use in our scripts.

The easiest way to do this with Chrome is by using the Developer Tools:

  • Open the developer tools (Windows: [.inline-code] F12[.inline-code] , Mac: [.inline-code] Cmd+Opt+J[.inline-code] )
  • Go to the Network tab
  • Do the necessary navigation tasks in Chrome
  • Locate the entry for the page we want
  • Right Click on the entry and choose Copy > Copy as cURL in the context menu

The cURL command reproducing the Chrome request will be in the clipboard, available in the command line or our scripts.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.