Sending Cookies with Curl [Node.js Code]

Cookies are passed to Curl with the --cookie "Name=Value" command line parameter. Curl automatically converts the given parameter into the Cookie: Name=Value request header. Cookies can be sent by any HTTP method, including GET, POST, PUT, and DELETE, and with any data, including JSON, web forms, and file uploads. In this Curl Send Cookies example, we are sending cookies to the ReqBin echo URL. Click Run to execute Curl Send Cookies example online and see results. The Node.js code was automatically generated for the Curl Send Cookies example.
Sending Cookies with Curl [Node.js Code] Run
curl -b "cookie_name=cookie_value" https://reqbin.com/echo

Updated: Viewed: 72376 times
Node.js code for Curl Send Cookies example

Node.js code for Curl Send Cookies Example

This Node.js code snippet was generated automatically for the Curl Send Cookies example.
<< Back to the Curl Send Cookies example

What is Curl?

The Curl is a command-line tool available for Linux, Windows, and macOS and a cross-platform library (libcurl) that can be used with almost any application written in nearly any programming language. Curl uses URL syntax to transfer data to and from servers. With Curl, you can upload or download data, submit web forms, and make API requests using over 25 protocols, including HTTP, HTTPS, FTP, and SFTP.

What is Cookie?

Cookies are small blocks of data sent from a website and stored by a web browser on a user's device. They allow web servers to store state information or track a user's online activity. Cookies are created by the server, tagged with a unique identifier for the user and device. Browsers send cookies back to the server with each subsequent request, facilitating user session management, tracking, and personalization.

Curl Send Cookies Example
GET /echo HTTP/1.1
Host: reqbin.com
Cookie: Name=Value

How set Cookie with Curl?

The following is a general form of the Curl command for sending a Cookies request :

Curl Send Cookies Syntax
curl --cookie "Name=Value" [URL]

What is cookie-jar?

The -c (or --cookie-jar) command-line option specifies the filename where Curl should write all cookies after the operation completes. Curl will write all the cookies from its in-memory cookie store to the specified file at the end of the process. If there are no cookies, Curl will not create the specified file. Below is an example of the syntax for saving cookies with Curl:

Curl Save Cookies Syntax
curl -c cookies.txt [URL]

How to set cookies for Curl?

To set cookies, you can use the -b (or --cookie) command-line switch to pass cookies to Curl. Below is an example of setting cookies for Curl:

Curl Send Cookies Syntax
curl -b "name=value" [URL]

Curl Send Cookies Examples

The following are examples of sending cookies using Curl:

Sending Cookies to Curl

You can use the -b command line option to send cookies with Curl. Below is an example of sending a cookie with Curl to the ReqBin echo URL:

Curl Send Cookies Example
curl -b "cookie_name=cookie_value" https://reqbin.com/echo

Sending Multiple Cookies

To send multiple cookies to Curl, you can use the -b flag and separate the cookies with a semicolon:

Curl Send Multiple Cookies Example
curl -b "cookie1_name=cookie1_value;cookie2_name=cookie2_value" https://reqbin.com/echo

Using a Cookie File

You can also store cookies in a file and use the -b option to specify the cookie's file:

Curl Using a Cookie File Example
curl -b cookies.txt https://reqbin.com/echo

How to use Сookies with Libcurl?

Libcurl is a free client library that can add the same capabilities to your application as the Curl command-line tool. Libcurl is portable, thread-safe, IPv6 compatible, and can be used on many platforms, including Windows and Linux. It also has bindings for many popular programming languages, including C++, JavaScript, PHP, Python, and others. Libcurl offers several ways to enable and interact with cookies in your application:

Options Action
CURLOPT_COOKIE Provide a cookie header to be sent to the server.
CURLOPT_COOKIEFILE Read cookies from the given cookie jar file.
CURLOPT_COOKIEJAR Save cookies to the specified cookie jar file.
CURLOPT_COOKIELIST Provide details of one cookie.
CURLINFO_COOKIELIST Extract cookie information from cookie storage.

See also

Generate code snippets for Node.js and other programming languages

Convert your Curl Send Cookies request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Node.js code generator.