Curl, short for Client for URLs, is a powerful command-line tool that allows users to make requests to and interact with various web services. It supports a wide range of protocols, making it a versatile tool for developers and system administrators. In this guide, we will explore the basics of using the Curl command, accompanied by practical examples.
Installation
Before diving into Curl, ensure it is installed on your system. On Linux, you can typically install it using the package manager. For example, on Ubuntu:
sudo apt-get update
sudo apt-get install curl
On macOS, you can use Homebrew:
brew install curl
For Windows users, you can download the executable from the official Curl website.
Making Simple GET Requests
One of the fundamental use cases for Curl is making HTTP GET requests. Let’s start with a simple example:
curl https://www.example.com
This command fetches the content of the specified URL and displays it in the terminal. You can replace the URL with any other web address.
Handling HTTP Headers
Curl allows you to include custom headers in your requests. This is useful for scenarios where you need to pass additional information to the server. Here’s an example:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/data
In this command, the -H
flag is used to add a custom header to the request, providing an access token for authentication.
Sending POST Requests
To send data to a server using a POST request, you can use the --data
option. For instance:
curl --data "username=johndoe&password=secretpassword" https://api.example.com/login
This command sends a POST request to the specified URL with the provided form data.
Handling Responses
Curl provides options to save the response to a file or display only specific information. For example:
curl -o output.html https://www.example.com
This command saves the content of the URL to a file named output.html
in the current directory.
Verbose Mode for Debugging
When troubleshooting or debugging, the verbose mode (-v
or --verbose
) can be helpful. It provides detailed information about the request and response:
curl -v https://www.example.com
Handling Authentication
Many web services require authentication to access certain resources. Curl supports various authentication methods, such as Basic Authentication. Here’s an example:
curl -u username:password https://api.example.com/data
In this command, the -u
flag is used to provide a username and password for Basic Authentication.
Uploading Files with Curl
Curl allows you to upload files to a server easily. For instance, to upload a file to a specific endpoint:
curl -F "file=@/path/to/local/file.txt" https://api.example.com/upload
Here, the -F
flag is used to specify a file to upload, and the @
symbol indicates the local file path.
Following Redirects
When a server responds with a redirect, Curl, by default, does not follow it. To enable automatic redirection, use the -L
flag:
curl -L https://www.example.com
This command instructs Curl to follow redirects, providing the final content after all redirections.
Limiting Connection Time
To set a maximum time for the entire request, you can use the --max-time
option. For example, to limit the request to 10 seconds:
curl --max-time 10 https://api.example.com/data
This is useful to prevent requests from running indefinitely.
HTTP Methods other than GET and POST
Curl supports various HTTP methods, such as PUT, DELETE, and PATCH. You can specify the method using the -X
flag. For example:
curl -X PUT --data "key=value" https://api.example.com/resource
This command sends a PUT request with data to update a resource.
Handling Cookies
Curl can handle cookies for you. To save cookies from a response and send them with subsequent requests, use the --cookie-jar
and --cookie
options:
curl --cookie-jar cookies.txt https://www.example.com/login
curl --cookie cookies.txt https://www.example.com/dashboard
The first command saves cookies to a file, and the second command uses those cookies in a subsequent request.