The curl
command in Linux is a powerful tool for transferring data from or to a server using various protocols like HTTP, HTTPS, FTP, and more. It is commonly used for testing APIs, downloading files, and interacting with web services.
Basic Syntax
curl [options] [URL]
[options]
: Flags and parameters that modifycurl
‘s behavior.[URL]
: The web address or server location you want to access.
Common Use Cases and Examples
1. Fetch a Web Page
To retrieve the contents of a webpage:
curl https://example.com
2. Save the Output to a File
Use the -o
or -O
option to save the content:
-o [filename]
: Save with a custom file name.-O
: Save with the original file name.
curl -o output.html https://example.com
curl -O https://example.com/index.html
3. Download a File
Download a file from a URL:
curl -O https://example.com/file.zip
4. Follow Redirects
By default, curl
doesn’t follow HTTP redirects. Use the -L
option:
curl -L https://example.com/redirect
5. Send GET Request
By default, curl
sends a GET request:
curl https://api.example.com/data
6. Send POST Request
Use the -X POST
option and pass data with -d
:
curl -X POST -d "param1=value1¶m2=value2" https://api.example.com/submit
7. Pass JSON Data in POST Request
Send JSON data with the -H
option for headers:
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/json
8. Download Multiple Files
Provide multiple URLs to download multiple files:
curl -O https://example.com/file1.zip -O https://example.com/file2.zip
9. Check HTTP Response Headers
Use the -I
option to fetch only the HTTP headers:
curl -I https://example.com
10. Limit Download Speed
Limit the download speed with the --limit-rate
option:
curl --limit-rate 100K https://example.com/file.zip
11. Resume a Download
Use the -C
option to resume a download:
curl -C - -O https://example.com/largefile.zip
12. Use Proxy
To access a URL via a proxy:
curl -x http://proxyserver:port https://example.com
13. Authenticate with Username and Password
For basic authentication:
curl -u username:password https://example.com
14. Specify Custom HTTP Headers
Use the -H
option to add custom headers:
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data
15. Upload a File
Use the -F
option to upload a file:
curl -F "file=@path/to/file.txt" https://example.com/upload
16. View Response in Verbose Mode
Enable verbose mode with -v
:
curl -v https://example.com
17. Test SSL Connection
Check the SSL certificate details of a website:
curl -v https://example.com
18. Send Data with PUT Request
Use the -X PUT
option to send data to a server:
curl -X PUT -d "param1=value1" https://api.example.com/update
19. Use Cookies
Save cookies to a file and send them in subsequent requests:
curl -c cookies.txt https://example.com
curl -b cookies.txt https://example.com
20. Measure Response Time
Use the -w
option to display response time metrics:
curl -w "Time taken: %{time_total}s\n" -o /dev/null -s https://example.com
Common Options Summary
Option | Description |
---|---|
-o |
Save output to a specific file. |
-O |
Save output with the same file name as the source. |
-L |
Follow redirects. |
-I |
Fetch HTTP headers only. |
-v |
Enable verbose output. |
-u |
Provide username and password for authentication. |
-H |
Specify custom HTTP headers. |
-X |
Specify HTTP method (e.g., GET, POST, PUT). |
-d |
Send data in a request. |
-F |
Submit form data or files. |
-C |
Resume an interrupted download. |
--limit-rate |
Limit download speed. |
Conclusion
The curl
command is a versatile tool for working with web services, downloading files, and testing APIs. With its wide range of options, it simplifies data transfer and HTTP communication. Let me know if you’d like to dive deeper into any specific use case! 😊