Sending Cookies with Curl

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.
Sending Cookies with Curl Run
curl --cookie "Name=Value"
Updated: Viewed: 43744 times

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 computer. Cookies are placed on the device used to access the website (computer, mobile phone, etc.). Several cookies may be placed on the same computer during a session. Browsers send cookies back to the server with each subsequent request, allowing the server to determine if the request came from the same browser or not.

Cookies enable web servers to store state information on a user's device or track a user's online activity. The data stored in cookies are created by the server when it connects to the server. This data is tagged with an identifier that is unique to user and user's computer. Cookies are primarily used for user session management, user tracking, and personalization.

Curl Send Cookies Example
GET /echo HTTP/1.1
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 report to the specified file all the cookies from its in-memory cookie store at the end of the process. If there are no cookies, Curl will not create the specified file. Below is an example of saving cookie syntax 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]

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, Linux, and 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 Curl Send Cookies Example

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

// try get IpInfo ASAP! getIpInfo($.noop);