cURL is a command-line tool and library used to transfer data with URLs. It's essentially a way to communicate with servers from your terminal, allowing you to download files, submit forms, interact with APIs, and more.
What is cURL?
- Client URL (cURL): The acronym stands for "Client URL."
- Data Transfer: cURL enables data transfer to and from servers.
- Command-Line Tool: Primarily operated through the command line or terminal.
- Library: Also available as a library (libcurl) for integration into programming languages.
- Versatile: Supports a wide range of protocols including HTTP, HTTPS, FTP, SFTP, SMTP, and more.
How do you use cURL?
The basic syntax for using cURL is:
curl [options] [URL]
Here's a breakdown with examples:
1. Basic Usage: Downloading a File
The simplest use case is downloading a file.
curl https://www.example.com/image.jpg > image.jpg
This command downloads the image located at https://www.example.com/image.jpg
and saves it as image.jpg
in your current directory. The >
redirects the output to a file.
2. Getting the Contents of a Webpage
To view the HTML content of a webpage:
curl https://www.example.com
This will display the HTML source code of the https://www.example.com
webpage in your terminal.
3. Sending Data with POST Requests
cURL is frequently used to interact with APIs by sending POST requests with data.
curl -X POST -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}' https://api.example.com/resource
-X POST
: Specifies the HTTP method as POST.-H "Content-Type: application/json"
: Sets theContent-Type
header to indicate that you are sending JSON data.-d '{"key1": "value1", "key2": "value2"}'
: Provides the data to be sent in the request body. The data here is a JSON string.https://api.example.com/resource
: The URL of the API endpoint.
4. Setting Headers
You can set custom headers using the -H
option. This is often needed for API authentication or specifying the desired response format.
curl -H "Authorization: Bearer YOUR_API_KEY" https://api.example.com/data
This example sends an "Authorization" header with a bearer token.
5. Following Redirects
If a URL redirects, you can use the -L
option to follow the redirect:
curl -L https://example.com
6. Verbose Mode
Use the -v
option for verbose output, which displays detailed information about the request and response, including headers and connection information. This is useful for debugging.
curl -v https://www.example.com
7. Authentication
cURL supports various authentication methods. Here's an example of basic authentication:
curl -u username:password https://example.com/protected
Common cURL Options Summary
Option | Description | Example |
---|---|---|
-X |
Specifies the HTTP method (GET, POST, PUT, DELETE) | curl -X POST https://api.example.com |
-H |
Adds a header to the request | curl -H "Content-Type: application/json" https://api.example.com |
-d |
Sends data with the request | curl -d "param1=value1¶m2=value2" https://api.example.com |
-u |
Specifies username and password for authentication | curl -u username:password https://example.com/protected |
-L |
Follows redirects | curl -L https://example.com |
-v |
Verbose mode (displays detailed information) | curl -v https://www.example.com |
-o |
Specifies the output file name | curl https://www.example.com/file.txt -o output.txt |
-O |
Uses the remote file name for the output file | curl -O https://www.example.com/file.txt (saves as file.txt) |
Conclusion
cURL is a powerful and versatile command-line tool that provides developers with a simple yet effective way to interact with web servers and APIs. Its flexibility and broad protocol support make it an essential tool for web development, testing, and automation.