How to use cURL Command?
The curl command is a versatile tool for making HTTP requests and interacting with various protocols. Here’s a basic overview of how to use the curl command with some common options:

1. Making a Simple GET Request
To make a simple GET request to a URL, use the following syntax:
curl [URL]
For example: cURL Command
curl https://www.example.com
This command will display the HTML content of the specified webpage in the terminal.
2. Specifying HTTP Method
You can specify the HTTP method using the -X option. For example, to make a POST request:
curl -X POST [URL]
3. Sending Data with POST Request
To send data with a POST request, you can use the -d option followed by the data you want to send:
curl -X POST -d "key1=value1&key2=value2" [URL]
4. Adding Headers
You can include custom headers using the -H option:
curl -H "Content-Type: application/json" [URL]
5. Following Redirects
To automatically follow redirects, use the -L option:
curl -L [URL]
6. Saving Output to a File
You can save the output to a file using the -o option:
curl -o output.html https://www.example.com
7. Displaying HTTP Response Headers
To display the HTTP response headers along with the content, use the -i option:
curl -i https://www.example.com
8. Handling Cookies
To handle cookies, use the -b option to send cookies and the -c option to save cookies to a file:
curl -b "cookie1=value1; cookie2=value2" -c cookies.txt [URL]
9. Insecure SSL Connections
If you are making requests to a server with a self-signed SSL certificate, you can use the -k or --insecure option to ignore SSL verification:
curl -k [URL]
10. Displaying Progress
To display a progress bar during the transfer, use the -# option:
curl -# [URL]
Example with Multiple Options
curl -X POST -H "Content-Type: application/json" -d '{"key": "value"}' https://api.example.com
This command makes a POST request to the specified API endpoint with a JSON payload and a custom header.
The curl command is a powerful tool with numerous options for making HTTP requests and interacting with various protocols. Here’s an overview of some common curl command options:
Basic Options
- URL:
curl [URL]: Makes a GET request to the specified URL.
- HTTP Methods:
-X, --request [HTTP_METHOD]: Specifies the HTTP method (GET, POST, PUT, DELETE, etc.).
Data Transfer
- Data Sending:
-d, --data [DATA]: Sends data in a POST request.--data-urlencode [DATA]: URL-encodes data for a POST request.
- Form Submission:
-F, --form [KEY=VALUE]: Sends data as a form in a multipart/form-data POST request.
- File Upload:
--upload-file [FILE]: Uploads a file using a PUT request.
- Follow Redirects:
-L, --location: Follows HTTP redirects.
Headers:
- Custom Headers:
-H, --header [HEADER]: Adds custom headers to the request.
Output Control
- Save to File:
-o, --output [FILE]: Saves output to a file.
- Display Headers:
-i, --include: Includes the HTTP headers in the output.
SSL/TLS
- Ignore SSL Certificate Verification:
-k, --insecure: Allows connections to SSL sites without certificates.
Authentication
- Basic Authentication:
-u, --user [USERNAME:PASSWORD]: Specifies the username and password for basic authentication.
- API Key Authentication:
--header "API-Key: [KEY]": Sends an API key as a header.
Cookies
- Send Cookies:
-b, --cookie [COOKIE]: Sends cookies in the request.
- Save Cookies:
-c, --cookie-jar [FILE]: Saves cookies to a file.
Progress
- Progress Bar:
-#: Displays a progress bar during transfers.
Other
- User Agent:
-A, --user-agent [AGENT]: Specifies the User-Agent string.
- Timeout:
--connect-timeout [SECONDS]: Sets the maximum time allowed for the connection.
- Verbose Output:
-v, --verbose: Provides more detailed output for debugging.
Example Usage
# Basic GET request
curl https://www.example.com
# POST request with data
curl -X POST -d "key1=value1&key2=value2" https://api.example.com
# Sending JSON data
curl -X POST -H "Content-Type: application/json" -d '{"key": "value"}' https://api.example.com
Useful Links
https://sanchitgurukul.com/basic-networking
https://sanchitgurukul.com/network-security
