How to Use cURL
Using cURL
cURL (short for Client for URLs) is a command-line tool and library used to transfer data to or from a server, using various supported protocols like HTTP, FTP, SMTP, and more. In this article, we will guide you through the process of using cURL on your command line interface.
Step 1: Install cURL (skip if already installed)
- For Windows users:
- Download the cURL executable from the official website (https://curl.se/download.html) and extract it to a folder.
- Add the cURL executable path to your system’s PATH environment variable.
- For Linux users:
- Run the following command in your terminal:
sudo apt-get install curl
- Run the following command in your terminal:
- For macOS users:
- cURL is pre-installed on macOS, so no installation is necessary.
Step 2: Open your command line interface
- For Windows users, open the Command Prompt or PowerShell.
- For Linux and macOS users, open the Terminal.
Step 3: Basic GET request
- To do a simple GET request using cURL, type the following command:
curl [URL]
Replace [URL] with the actual URL you want to make a request to. - Example:
curl https://www.example.com
Step 4: Specify a different HTTP method
- By default, cURL performs a GET request, but you can specify a different HTTP method using the -X flag.
- Example:
curl -X POST https://www.example.com
Step 5: Include request headers
- To include custom headers in your request, use the -H flag followed by the header information.
- Example:
curl -H "Content-Type: application/json" -H "Authorization: Bearer [token]" https://www.example.com
Step 6: Send data in the request body
- To send data in the request body, use the -d flag followed by the data.
- Example:
curl -X POST -d "username=admin&password=1234" https://www.example.com/login
Step 7: Save the response to a file
- To save the response to a file instead of displaying it in the terminal, use the -o flag followed by the file path.
- Example:
curl -o response.json https://www.example.com/api/data
These are the basic steps to get started with cURL. It’s a versatile tool that can be used for various tasks, including testing APIs, downloading files, and sending HTTP requests. Explore the official cURL documentation (https://curl.se/docs/) for more advanced options and functionalities.