curl Command
curl is a versatile command-line tool for transferring data with URLs. It supports a vast range of protocols, including HTTP, HTTPS, FTP, and more, making it a "Swiss Army knife" for interacting with web services and APIs.
Basic Usage
Fetch Content of a URL
curl https://example.comDownloads the content from the given URL and displays it in the terminal.
Save Output to a File
curl -o page.html https://example.comThe -o flag saves the downloaded content to a specified file (page.html).
Download and Save with Original Filename
curl -O https://example.com/file.zipThe -O (uppercase) flag saves the file using the name from the URL (file.zip).
HTTP Interaction
Show HTTP Headers Only
curl -I https://example.comThe -I flag (HEAD request) fetches and displays only the HTTP headers, without downloading the body. Useful for checking status codes or content type.
Follow Redirects
curl -L http://google.comThe -L flag tells curl to follow any "Location" headers (redirects) until it reaches the final destination.
Send POST Request
curl -X POST -d "name=John&age=30" https://example.com/formThe -X POST specifies the request method, and -d provides the data to be sent in the request body.
Send JSON Data
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/dataA common use case for testing APIs. -H sets a custom header, and -d sends the JSON string.
Checking Your Public IP Address
You can use curl to query external services that return your public IP address.
Get Public IPv4
curl ifconfig.meRetrieves and displays your public IPv4 address from the ifconfig.me service. Other similar services include icanhazip.com.
Get Public IPv6
curl -6 ifconfig.meForces curl to use IPv6 to retrieve and display your public IPv6 address (if available).