In the realm of command-line utilities, few tools are as versatile and powerful as Curl. Short for “Client URL,” Curl is a command-line tool that enables users to transfer data to or from a server supporting various protocols like HTTP, HTTPS, FTP, FTPS, SCP, SFTP, LDAP, and more. In this blog, we’ll dive into the world of Curl commands and explore some essential use cases that will empower you to harness its full potential.
Curl is installed by default on most Linux distributions. Type curl into your terminal and press enter to see if it’s installed on your machine. It will display a “command not found” message if it is not installed. To install it on your machine, Please check steps here.
List of curl command with examples
1. Retrieving Web Pages
One of the fundamental use cases of Curl is fetching web pages from URLs. Whether you want to quickly view the HTML content of a website or save it to a local file, Curl has got you covered.
To fetch and display the HTML content of a webpage:
# curl https://www.example.com
To save the webpage as a file (e.g., index.html):# curl https://www.maggiminutes.com
# curl -o index.html https://www.example.com
2. Downloading Files
Downloading files from the internet is a breeze with Curl command. You can download files using various protocols and even resume interrupted downloads.
To download a file and save it with its original name:
# curl -O https://www.example.com/file.zip
To download a file and specify the local filename:
# curl -o local_filename.zip https://www.example.com/file.zip
If you have a partially downloaded file, use the -C – option to resume the download
# curl -O -C - https://www.example.com/file.zip
3. Uploading Files
Curl command not only downloads files but can also upload local files to a server using HTTP POST requests.
To upload a file to a server:
# curl -X POST -F "[email protected]" https://upload.example.com
Sending HTTP Requests
Curl command is the go-to tool for developers when testing APIs or interacting with web services. It allows you to send custom HTTP requests, set headers, and handle various methods such as GET, POST, PUT, DELETE etc.
To send a GET request:
# curl -X GET https://api.example.com/data
To send a POST request with data:
# curl -X POST -d "name=user&age=30" https://api.example.com/users
To send a POST request with JSON data:
# curl -X POST -d '{"email":"[email protected]", "name": ["user"]}' -H 'Content-Type: application/json' https://api.example.com/users
To send a POST request with JSON file:
# curl -X POST -d @data.json -H 'Content-Type: application/json' https://api.example.com/users
Using the PUT method, you can make “update” requests with JSON data:
# curl -X PUT -d '{"email":"[email protected]", "name": ["user"]}' -H "Content-Type: application/json" https://api.example.com/users
Using the PUT method, you can make “update” requests with JSON file:
# curl -X PUT -d @data.json -H "Content-Type: application/json" https://api.example.com/users
You can send deletion requests to an API using the DELETE method:
# curl -X DELETE https://api.example.com/users
Authentication
Testing authentication mechanisms is crucial during development. Curl allows you to easily test Basic Authentication, OAuth, and more.
For Basic Authentication:
# curl -u username:password https://api.example.com
For OAuth with a token:
# curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/data
Inspecting Headers
Sometimes, you may need to inspect the response headers to check server configurations and debug caching issues.
To view response headers:
# curl -I https://www.example.com
Testing Protocol Support
Use curl to evaluate protocol support because of the vast number of protocols supported.
# curl -v --tlsv1.3 https://www.example.com
# curl -v --http2 https://www.example.com
Disabling Certificate Checks
When connecting via HTTPS, Curl, examines certificates by default. Use the -k to disable non trusted certificate checking.
# curl -k https://www.example.com
Conclusion
Curl commands are a must-have skill for any developer or system administrator working with web services, APIs, or data transfer. Its simplicity, versatility, and robustness make it an invaluable tool for fetching web pages, testing APIs, downloading and uploading files, and more. By mastering these essential Curl commands, you’ll be better equipped to streamline your development workflows, troubleshoot network interactions, and save valuable time on data transfer tasks. So go ahead, embrace Curl, and unlock the power of the command line! Happy Curling!