468
curl is a command-line tool for transferring data using various protocols like HTTP, HTTPS, FTP, and SFTP. It’s widely used for API testing, downloading files, and debugging network requests.
Basic Usage
| Command | Description |
|---|---|
| curl https://example.com | Fetch and display the webpage contents. |
| curl -o file.html https://example.com | Save output to a file. |
| curl -O https://example.com/file.zip | Download a file (keep original filename). |
| curl -L https://example.com | Follow redirects. |
| curl -I https://example.com | Fetch only HTTP headers. |
| curl -s https://example.com | Silent mode (no progress output). |
Example: Download a file and rename it
curl -o myfile.zip https://example.com/file.zip
Sending HTTP Requests
| Command | Description |
|---|---|
| curl -X GET URL | Send a GET request (default). |
| curl -X POST URL | Send a POST request. |
| curl -X PUT URL | Send a PUT request. |
| curl -X DELETE URL | Send a DELETE request. |
| curl -d “param1=value1¶m2=value2” -X POST URL | Send form data. |
| curl -H “Custom-Header: value” URL | Add custom HTTP headers. |
Example: Send a POST request with data
curl -X POST -d "username=admin&password=1234" https://example.com/login
Authentication
| Command | Description |
|---|---|
| curl –user username:password URL | Basic authentication. |
| curl -u user:pass URL | Alternative basic authentication. |
| curl -H “Authorization: Bearer <token>” URL | Bearer token authentication. |
Example: API request with a Bearer Token
curl -H "Authorization: Bearer mysecrettoken" https://api.example.com/data
Uploading & Downloading Files
| Command | Description |
|---|---|
| curl -T myfile.txt ftp://example.com | Upload a file via FTP. |
| curl -T myfile.txt -u user:pass ftp://example.com | FTP upload with authentication. |
| curl -o file.zip URL | Download a file and save it. |
Example: Upload a file to an FTP server
curl -T myfile.txt -u user:pass ftp://example.com/uploads/
Handling JSON Data
| Command | Description |
|---|---|
| curl -H “Content-Type: application/json” URL | Set JSON content type. |
| curl -d ‘{“key”:”value”}’ -H “Content-Type: application/json” -X POST URL | Send JSON payload. |
Example: Send JSON data in a POST request
curl -X POST -H "Content-Type: application/json" -d '{"name":"Alice","age":25}' https://api.example.com/users
Debugging & Logging
| Command | Description |
|---|---|
| curl -v URL | Verbose mode (detailed output). |
| curl –trace trace.txt URL | Save detailed trace to a file. |
| curl –trace-ascii trace.log URL | Save ASCII trace logs. |
Example: Debug a request
curl -v https://example.com
Testing APIs
| Command | Description |
|---|---|
| curl -X GET https://api.example.com/data | Fetch API data. |
| curl -X DELETE https://api.example.com/users/1 | Delete user ID 1. |
Example: GET request with query parameters
curl "https://api.example.com/data?user=Alice&age=25"
Additional Useful Flags
| Command | Description |
|---|---|
| curl –limit-rate 100k URL | Limit download speed. |
| curl –max-time 10 URL | Timeout after 10 seconds. |
| curl -C – URL | Resume a paused download. |
Example: Limit speed to 200KB/s
curl --limit-rate 200k -O https://example.com/file.zip
Summary
| Feature | Command Example |
|---|---|
| Basic Request | curl https://example.com |
| Download File | curl -O URL |
| Follow Redirects | curl -L URL |
| Send POST Request | curl -X POST -d “param=value” URL |
| Add Headers | curl -H “Header: value” URL |
| Upload File | curl -T file.txt ftp://example.com |
| Use Authentication | curl -u user:pass URL |
| Debug Request | curl -v URL |
This Linux curl Cheat Sheet will help you download files, send API requests, authenticate, and debug HTTP traffic!
