Curl Tutorial For Beginners
Nov 16 2024
Curl is a powerful command-line tool every developer and system administrator should have in their toolkit. So why wait? Let's explore Curl's capabilities!
What is Curl?
cURL, or Client URL, is a command-line tool for transferring data using various protocols. It was developed by Daniel Stenberg, a Swedish programmer. cURL is an open-source project, and its source code can be found here.
Curl is mostly known for its capabilities of interacting with servers and APIs over the HTTP(S) protocol. But its capabilities extend far beyond that. It supports various protocols like FTP, FTPS, SFTP, SMTP, RTSP, and others. This allows you to download files, sending emails, test APIs, and even submit forms directly from your command line.
How To Install Curl
Most systems come with curl pre-installed. To check if you have it, simply type:
curl --version
If it’s not installed, you can download it here depending on your operating system.
Basic Usage
The most basic use of cURL is to fetch a web page, that is, by sending a GET request.
curl https://www.example.com
This command will retrieve the HTML content of the specified URL and display it in your terminal.
In addition, Curl offers numerous options to customize requests, for example, To save the result to a file, use the -o flag:
curl -o index.html https://example.com
This will save the output to index.html instead of printing it on the terminal.
Some URLs may redirect you. Use -L to follow redirects automatically:
curl -L https://example.com
When working with APIs, you may need to add headers like Authorization. You can add headers with the -H option:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/data
Making Post Request
To send data, use the -X POST option along with -d for data:
curl -X POST -d "name=John&age=30" https://api.example.com/register
For JSON data, specify the Content-Type header:
curl -X POST -H "Content-Type: application/json" -d '{"name": "John", "age": 30}' https://api.example.com/register
Conclusion
Curl is a powerful tool for making HTTP requests from the command line in Bash. Whether you need to retrieve data from a website or interact with a web API, Curl simplifies the process and provides numerous options for customization.
With the basics covered in this article, you can start using Curl to perform a wide range of HTTP-related tasks in your Bash scripts and daily workflow.
Thank you for reading.