curl — Transferring Data and Testing APIs from the Command Line¶
curl (Client URL) is one of the most powerful command-line tools for transferring data using URLs. It supports numerous protocols, including HTTP, HTTPS, FTP, SFTP, and more. Linux administrators, DevOps engineers, Cloud Architects, API developers, and Site Reliability Engineers (SREs) use
curldaily to test web servers, interact with REST APIs, download files, verify application health, and troubleshoot network services.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
curlcommand - Make HTTP and HTTPS requests
- Test REST APIs
- Download files
- Send request headers
- Send JSON data
- Authenticate requests
- Troubleshoot web services
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management
- Module 5 – Users and Groups
- Module 6 – Process Management
- Module 7 – Package Management
- Module 8 Lessons 1–8
Why Learn curl?¶
Imagine:
- Your API is returning errors.
- Kubernetes readiness probes are failing.
- You need to verify a REST endpoint.
- A website is unreachable.
- You need to download a configuration file.
The first command many engineers use is:
What is curl?¶
curl stands for:
It transfers data between a client and a server.
Supported protocols include:
- HTTP
- HTTPS
- FTP
- FTPS
- SFTP
- SCP
- SMTP
- LDAP
- MQTT
- File
How curl Works¶
Basic Request¶
Request a web page.
The response body is displayed in the terminal.
View Only HTTP Headers¶
Example:
Follow Redirects¶
Some websites redirect requests.
-L tells curl to follow redirects automatically.
Download a File¶
Save using the remote filename.
Save with a custom filename.
Display Verbose Output¶
Useful for troubleshooting.
Shows:
- DNS lookup
- TCP connection
- TLS handshake
- Request headers
- Response headers
Silent Mode¶
Suppress progress output.
Useful in shell scripts.
Send a GET Request¶
GET is the default HTTP method.
Send a POST Request¶
Send JSON Data¶
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice","role":"admin"}'
Send Custom Headers¶
Basic Authentication¶
Bearer Token Authentication¶
Send Query Parameters¶
View HTTP Status Code¶
Example output:
Common status codes:
| Code | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 301 | Moved Permanently |
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 500 | Internal Server Error |
Test HTTPS Certificates¶
To ignore certificate verification (testing only):
Warning: Avoid using
-kin production because it disables TLS certificate verification.
Common Commands¶
Basic request.
Headers only.
Verbose mode.
Download file.
POST request.
Real Production Examples¶
Check website availability.
Verify Kubernetes health endpoint.
Call REST API.
Download a configuration file.
Verify HTTP status.
Production Perspective¶
curl is widely used for:
- REST API testing
- Kubernetes health checks
- Load balancer verification
- CI/CD pipelines
- Monitoring scripts
- Cloud service testing
- Authentication testing
- Application troubleshooting
It is one of the most frequently used networking tools in DevOps and SRE environments.
Hands-on Lab¶
Task 1¶
Request a web page.
Task 2¶
Display only response headers.
Task 3¶
Display verbose output.
Task 4¶
Download a file.
Task 5¶
Check the HTTP status code.
Task 6¶
Request a REST API.
Task 7¶
Send JSON data.
curl -X POST \
https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"message":"Hello Linux"}'
Task 8¶
Follow redirects.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
curl URL | GET request | API testing |
curl -I | Headers only | Server verification |
curl -O | Download file | Configuration download |
curl -o | Save with filename | Backup downloads |
curl -v | Verbose output | Troubleshooting |
curl -L | Follow redirects | Website testing |
curl -H | Send custom headers | API authentication |
curl -X POST | POST request | REST API testing |
Common curl Errors¶
| Error | Possible Cause |
|---|---|
Could not resolve host | DNS failure |
Connection refused | Service not running or port closed |
Connection timed out | Network or firewall issue |
SSL certificate problem | Invalid or untrusted certificate |
404 Not Found | Resource does not exist |
401 Unauthorized | Authentication required |
curl vs wget¶
| Feature | curl | wget |
|---|---|---|
| API Requests | ✅ Excellent | Limited |
| Download Files | ✅ | ✅ Excellent |
| Upload Data | ✅ | Limited |
| HTTP Methods | Multiple | Mostly GET |
| Custom Headers | ✅ | ✅ |
| Resume Downloads | Basic | Excellent |
Use curl for APIs and web services.
Use wget primarily for downloading files and websites.
Production Troubleshooting Scenario¶
Scenario
A Kubernetes application fails its readiness probe.
Investigation:
Check the health endpoint.
Response:
Check application logs.
The application cannot connect to its database.
After fixing the database connection:
Response:
The readiness probe now succeeds, and the application becomes available.
Best Practices¶
- Use
-Ito verify HTTP headers quickly. - Use
-vwhen troubleshooting connection problems. - Use
-sin automation scripts to suppress unnecessary output. - Verify HTTP status codes when testing APIs.
- Protect authentication credentials and tokens.
- Avoid using
-kexcept in controlled testing environments.
Common Mistakes¶
❌ Forgetting to follow redirects with -L.
✅ Remember to to follow redirects with -L.
❌ Exposing authentication tokens in shared scripts.
✅ Avoid this mistake: exposing authentication tokens in shared scripts.
❌ Ignoring HTTP status codes.
✅ Always review HTTP status codes.
❌ Using -k in production environments.
✅ Avoid using -k in production environments when a safer approach exists.
Interview Questions¶
Beginner¶
- What is
curlused for? - Which command displays only HTTP headers?
- How do you download a file?
- Which option enables verbose output?
Intermediate¶
- How do you send a POST request with JSON?
- What does the
-Hoption do? - How do you follow redirects?
- How do you retrieve only the HTTP status code?
Architect Level¶
- How would you use
curlto troubleshoot a failing REST API? - Why is
curlcommonly used in CI/CD pipelines? - How would you securely authenticate API requests using
curl?
Summary¶
In this lesson, you learned:
- The
curlcommand - HTTP and HTTPS requests
- REST API testing
- File downloads
- Authentication
- HTTP headers
- Status codes
- Production troubleshooting
curl is one of the most versatile networking tools available on Linux. It is indispensable for interacting with web servers, testing APIs, downloading files, and diagnosing application connectivity issues in modern production environments.
Key Takeaways¶
curlis the standard command-line tool for transferring data using URLs.- Use
curl -Ito view HTTP headers. - Use
curl -vfor detailed troubleshooting. - Use
curl -Hto send custom request headers. - Use
curl -Xto specify HTTP methods such as POST, PUT, or DELETE. curlis an essential tool for API testing, automation, and DevOps workflows.
What's Next?¶
wget — Downloading Files from the Command Line
You'll explore:
- Downloading files from the Internet
- Recursive downloads
- Resuming interrupted downloads
- Background downloads
- Mirroring websites
- Authentication
- Production download automation
While curl excels at interacting with APIs and web services, wget is optimized for downloading files and website content efficiently.