• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Use Cookies With cURL

Oscar Forero

Published: 3/5/2024

About Terminus

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 curl  command, you can use the -c  flag (short for --cookie-jar ) as follows:

 $ curl -c <file> <url>

Run in Warp

Where:

  • file  is the relative or absolute path to the file you want to store the cookies in.
  • url  is the URL the request is sent to.

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

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

Run in Warp

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 curl  command, you can use the -b  flag as follows:

 $ curl -b <file> <url>

Run in Warp

Easily retrieve this command using Warp’s AI Command Suggestions

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

Thumbnail for

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

Storing temporary cookies using an environment variable

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>)

Run in Warp

Where:

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

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

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

Run in Warp

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>

Run in Warp

Where:

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

For example, the following command will…:

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

Run in Warp

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

Sending synthetic cookies with cURL

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 curl .

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

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

Run in Warp

Where:

  • name  is the name of the cookie.
  • value  is the value of the cookie.

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

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

Run in Warp

Sending a request with multiple cookies

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

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

Run in Warp

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

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

Run in Warp

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

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

Run in Warp

Which is equivalent to this command:

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

Run in Warp

Removing stored cookies

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

 $ rm <file>

Run in Warp

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

 $ unset <variable>

Run in Warp

Using cookies stored in Chrome with cURL

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: F12 , Mac: Cmd+Opt+J )
  • 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.

Thumbnail for

Written by

Oscar Forero

Filed Under

Related Articles

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash
Thumbnail for Gabriel ManricksGabriel Manricks

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Bash

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Loop Through Files in Directory in Bash

Learn how to iterate over files in a directory linearly and recursively using Bash and Python.

BashPython
Thumbnail for Razvan LudosanuRazvan Ludosanu

How To Use sudo su

A quick overview of using sudo su

LinuxUnixBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Generate, Sign, and View a CSR With OpenSSL

Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.

BashLinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to use sudo rm -rf safely

We'll help you understand its components

BashLinuxUnix
Thumbnail for Neeran GulNeeran Gul

How to run chmod recursively

Using -R is probably not what you want

LinuxBashUnix
Thumbnail for Brett TerpstraBrett Terpstra

Run Bash Shell In Docker

Start an interactive shell in Docker container

DockerBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Curl Post Request

Use cURL to send data to a server

BashUnixLinux
Thumbnail for Zev StravitzZev Stravitz

Reading User Input

Via command line arguments and prompting users for input

BashLinuxUnix
Thumbnail for Amit JotwaniAmit Jotwani

Bash Aliases

Create an alias for common commands

BashLinuxUnix
Thumbnail for Brett TerpstraBrett Terpstra

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null