
How to Master the curl Command in Linux?
Mastering the curl
Command in Linux
The curl
command is a powerful tool that allows you to transfer data to and from servers using various protocols, including HTTP, HTTPS, FTP, and SCP. It’s widely used for everything from making simple requests to complex API calls.
Understanding curl
At its core, curl
is designed to:
- Transfer data from or to a server.
- Support a multitude of protocols, making it extremely versatile.
For detailed documentation, visit the official curl man page.
Basic Usage
Here are some foundational commands to get you started with curl
:
-
Make a Basic HTTP GET Request To fetch and display the contents of a webpage in the terminal:
curl https://example.com
-
Follow Redirects and Display Headers To automatically follow any
3xx
redirects and see both headers and content:curl -L -D - https://example.com
-
Download a File To save the output under the filename specified in the URL:
curl -O https://example.com/filename.zip
-
Send Form-Encoded Data For sending data via a POST request, you can use:
curl -X POST -d 'name=bob' http://example.com/form
Advanced Uses
-
Set Custom HTTP Headers You can send a request with custom headers, useful when working with APIs:
curl -H 'Authorization: Bearer token' https://example.com
-
Handle JSON Data To send data in JSON format, specify the appropriate Content-Type header:
curl -d '{"name":"bob"}' -H 'Content-Type: application/json' http://example.com/users/1234
-
Skip SSL Certificate Validation If you are dealing with self-signed certificates, you can skip validation:
curl -k https://example.com
-
Pass Client Certificates To authenticate with the server using a client certificate:
curl -E client.pem --key key.pem -k https://example.com
-
Resolve Hostnames to Custom IP Addresses To force
curl
to resolve a hostname to a specific IP:curl --resolve example.com:80:127.0.0.1 http://example.com
Conclusion
curl
is an indispensable tool for anyone working with web technologies. Whether you need to make simple GET requests or complex API interactions, understanding the capabilities of curl
will greatly enhance your command-line proficiency. Happy coding!