cURL (Client URL) is a powerful command-line tool and library used for transferring data with URLs. It supports multiple protocols, including HTTP, HTTPS, FTP, SCP, SFTP, and more. Developers and system administrators commonly use cURL for API interactions, testing endpoints, downloading files, and troubleshooting network issues.
In this guide, we'll cover everything you need to know about cURL, from installation to advanced usage.
Table of Contents
-
What is cURL?
-
Installing cURL
-
Basic cURL Commands
-
cURL with HTTP Methods (GET, POST, PUT, DELETE)
-
Sending Headers and Cookies
-
Handling Authentication with cURL
-
Uploading and Downloading Files
-
Working with JSON and APIs
-
Handling SSL/TLS Certificates
-
Debugging with cURL
-
Using cURL in PHP, Python, and Bash
-
cURL Alternatives
-
Conclusion
- What is cURL?
cURL is a free and open-source command-line utility for transferring data between a client and a server. It was created by Daniel Stenberg in 1996 and is widely used in web development, automation, and testing.
Key Features of cURL:
Supports multiple protocols (HTTP, FTP, SMTP, etc.).
Works on Linux, macOS, and Windows.
Allows sending custom headers, cookies, and authentication.
Can handle file uploads and downloads.
Supports SSL/TLS for secure communication.
- Installing cURL
On Linux (Ubuntu/Debian)
sudo apt update sudo apt install curl -y
On CentOS/RHEL
sudo yum install curl -y
On macOS
cURL is pre-installed on macOS. You can check the version using:
curl --version
If you need the latest version, install it using Homebrew:
brew install curl
On Windows
Download the latest cURL package from the official website. Alternatively, install it via Chocolatey:
choco install curl
- Basic cURL Commands
Here are some simple cURL commands:
Check cURL Version
curl --version
Fetch a Webpage
curl https://example.com
Save Output to a File
curl -o output.html https://example.com
Follow Redirects
curl -L https://example.com
Show HTTP Response Headers
curl -I https://example.com
- cURL with HTTP Methods
cURL allows you to make HTTP requests using different methods.
GET Request
curl -X GET https://api.example.com/data
POST Request (Sending Data)
curl -X POST -d "name=John&age=30" https://api.example.com/users
PUT Request (Updating Data)
curl -X PUT -d "name=John Doe" https://api.example.com/users/1
DELETE Request
curl -X DELETE https://api.example.com/users/1
- Sending Headers and Cookies
Sending Custom Headers
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/data
Sending Cookies
curl -b "session=abc123" https://example.com
- Handling Authentication with cURL
Basic Authentication
curl -u username:password https://api.example.com/login
OAuth Token Authentication
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/data
- Uploading and Downloading Files
Download a File
curl -O https://example.com/file.zip
Upload a File
curl -X POST -F "file=@/path/to/file.txt" https://api.example.com/upload
- Working with JSON and APIs
Sending JSON Data
curl -X POST -H "Content-Type: application/json" -d '{"name": "John", "age": 30}' https://api.example.com/users
Fetching JSON Data
curl -H "Accept: application/json" https://api.example.com/data
- Handling SSL/TLS Certificates
Ignore SSL Certificate Validation
curl -k https://example.com
Use a Custom Certificate
curl --cacert /path/to/cert.pem https://secure.example.com
- Debugging with cURL
Enable Verbose Mode
curl -v https://example.com
Show Request and Response
curl -i https://example.com
Log cURL Details
curl -o output.txt -s -w "%{http_code}\n" https://example.com
- Using cURL in Different Languages
PHP cURL Example
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Python cURL Example
import requests response = requests.get("https://api.example.com/data") print(response.text)
Bash Script Example
#!/bin/bash curl -X GET https://api.example.com/data
- cURL Alternatives
If cURL doesn't fit your needs, consider these alternatives:
Wget: Great for downloading files.
Postman: A GUI tool for API testing.
httpie: A user-friendly HTTP client.
- Conclusion
cURL is an essential tool for developers and system administrators. Whether you're making API requests, automating tasks, or debugging network issues, cURL provides flexibility and power.
If you're working with APIs frequently, mastering cURL can significantly improve your workflow. Try out the commands in this guide and explore its full potential!
Frequently Asked Questions (FAQs)
- What is the difference between cURL and Wget?
cURL is more versatile and supports multiple protocols, while Wget is optimized for downloading files.
- Can I use cURL in Windows?
Yes, you can install cURL on Windows using Chocolatey or download it from the official website.
- How do I save cURL output to a file?
Use the -o flag:
curl -o output.html https://example.com
- How do I debug cURL requests?
Use -v (verbose) mode:
curl -v https://example.com