Cookies are small pieces of data that are stored on a user's computer by a web browser. They are used in HTTP requests to send information from the client to the server and back. Cookies are used to store information about the user's session, such as their preferences, shopping cart contents, or authentication status. Cookies are also used to track users across different websites and to serve targeted ads.

Cookies are not used by cURL by default. However, cookies can become particularly handy when working with cURL. It can be used when simulating browser sessions for testing your web application, automating web requests to observe specific behaviors, or conducting in-depth vulnerability assessments to pinpoint potential security threats.

There are multiple methods for incorporating cookies with cURL. You can specify them as parameters in your requests or reference cookies pre-crafted and stored in a file. You can use a single cookie or multiple cookies at once in your requests.

  1. Open the terminal.
  2. Execute a cURL request for your target URL.

    $ curl https://www.example.com/
  3. Manually set the cookie's name and value using the -b or –cookie.

    $ curl -b "name=value" http://www.example.com/ -v 2>&1 | grep '^>'
    > GET / HTTP/1.1
    > Host: www.example.com
    > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
    > Accept: */*
    > Cookie: name=value
    > 

    Use the -b or –cookie parameter to set the cookie's name and value. The cookie's name and value must be separated by an equal (=) sign.

    Use the -v or –verbose parameter to display detailed information about the request and the server's response, and 2>&1 | grep '^>' parameter to filter the output. These are for demonstration purposes only and are not required for normal cURL requests.

  4. Use a semicolon as the separator to use multiple cookies.

    $ curl -b "name1=value1; name2=value2" http://www.example.com/ -v 2>&1 | grep '^>'
    > Host: www.example.com
    > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
    > Accept: */*
    > Cookie: name1=value1; name2=value2
    >
  5. Use a saved cookie file to add cookies to cURL requests.

    $ curl -b "/path/to/cookie-file.txt" https://www.example.com/

    cURL support the use of Netscape, Mozilla, and Lynx cookie files.

    Use the -b or –cookie parameter to specify the path to the cookie file.