How to Send a Cookie in a Curl Command
Sending a specific cookie key-value pair with a curl
command is a fundamental task for web developers and system
administrators testing APIs, debugging web sessions, or scraping web
data. This article provides a quick, direct guide on how to use the
-b (or --cookie) flag in curl to
send a single, custom cookie key-value pair directly in your HTTP
requests.
The Basic Syntax
To send a single cookie key-value pair, use the -b
option followed by the cookie name and value enclosed in quotation
marks.
The basic command structure is:
curl -b "cookie_name=cookie_value" URLStep-by-Step Example
Suppose you want to send an HTTP GET request to
https://example.com/api and authenticate it using a session
cookie where the key is session_id and the value is
abc123xyz.
Run the following command in your terminal:
curl -b "session_id=abc123xyz" https://example.com/apiUsing the Long-Form Flag
If you prefer more descriptive command-line arguments, you can use
the long-form --cookie flag instead of -b.
Both flags perform the exact same function:
curl --cookie "session_id=abc123xyz" https://example.com/apiHow It Works
When you run this command, curl automatically formats
the input into a standard HTTP request header. The server receives the
following header in the incoming request:
Cookie: session_id=abc123xyz