Linux curl Command — Transferring Data and Testing APIs¶
curl(Client URL) is one of the most powerful command-line tools used to transfer data between a client and a server using various network protocols. It is widely used for testing REST APIs, downloading data, uploading files, debugging HTTP/HTTPS requests, interacting with cloud services, Kubernetes APIs, and automating web-based workflows.curlsupports more than 20 protocols, including HTTP, HTTPS, FTP, SFTP, SCP, SMTP, LDAP, and more. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, Site Reliability Engineer (SRE), Network Engineer, and Security Engineer should master thecurlcommand.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
curlcommand - Send HTTP and HTTPS requests
- Test REST APIs
- Work with request headers
- Authenticate API requests
- Upload and download files
- Troubleshoot web services
Prerequisites¶
Complete:
- Linux
ipCommand - Linux
ssCommand - Linux
netstatCommand - Linux
tcpdumpCommand - Linux
tracerouteCommand - Linux
digCommand - Linux
nslookupCommand
Basic understanding of:
- HTTP
- HTTPS
- REST APIs
- JSON
Why Learn curl?¶
Suppose users report:
- API Not Responding
- Website Returns Error
- Authentication Failure
- Kubernetes API Issues
- Cloud Service Errors
Instead of opening a browser, Linux engineers use:
to verify:
- Is the server reachable?
- What HTTP status code is returned?
- What headers are sent?
- What response body is received?
- Does authentication work?
What is curl?¶
curl stands for:
It sends requests to servers and displays responses.
Supported protocols include:
- HTTP
- HTTPS
- FTP
- FTPS
- SFTP
- SCP
- SMTP
- LDAP
- MQTT
- Many others
Basic Syntax¶
Example:
Simple HTTP Request¶
Returns:
HTTPS Request¶
Uses Transport Layer Security (TLS) automatically.
Display Response Headers¶
Example output:
Display Headers and Body¶
Verbose Mode¶
Displays:
- Domain Name System (DNS) Resolution
- Transmission Control Protocol (TCP) Connection
- TLS Handshake
- HTTP Request
- HTTP Response
Useful for troubleshooting.
Follow Redirects¶
Automatically follows HTTP redirects.
Save Output to File¶
Save Using Remote Filename¶
Uses the filename from the URL.
Download Multiple Files¶
Specify HTTP Method¶
GET (default):
POST:
PUT:
DELETE:
Send JSON Data¶
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"Alice"}' \
https://api.example.com/users
Send Custom Headers¶
Basic Authentication¶
Bearer Token Authentication¶
Common for REST APIs and cloud platforms.
Upload a File¶
Upload JSON File¶
Display Only HTTP Status Code¶
Useful in monitoring scripts.
Ignore TLS Certificate Validation¶
Warning: Use
-konly for testing or trusted internal environments. Do not disable certificate validation in production.
Set Request Timeout¶
Stops the request after ten seconds.
Test REST API¶
Returns:
Enterprise Example¶
Test a production API.
Verify:
- Authentication
- Response Time
- Status Code
- Response Body
Cloud Perspective¶
Cloud engineers use curl to:
- Test Cloud APIs
- Verify Load Balancers
- Check Health Endpoints
- Retrieve Metadata
- Validate Service Availability
Kubernetes Perspective¶
Test Kubernetes API.
Health endpoint.
Ingress endpoint.
curl is widely used to verify:
- Services
- Ingress
- API Gateways
- Health Checks
Linux Perspective¶
Basic request.
Verbose mode.
Download file.
POST request.
curl -X POST \
-H "Content-Type: application/json" \
-d '{"test":"value"}' \
https://api.example.com
Common HTTP Status Codes¶
| Status Code | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 204 | No Content |
| 301 | Permanent Redirect |
| 302 | Temporary Redirect |
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 500 | Internal Server Error |
| 503 | Service Unavailable |
Common curl Commands¶
| Command | Purpose |
|---|---|
curl URL | GET request |
curl -I URL | Headers only |
curl -i URL | Headers and body |
curl -v URL | Verbose output |
curl -O URL | Download using remote filename |
curl -o file URL | Save with custom filename |
curl -X POST | POST request |
curl -H | Add headers |
curl -d | Send request body |
curl -u | Basic authentication |
Hands-on Lab¶
Task 1¶
Retrieve a web page.
Task 2¶
Display response headers.
Task 3¶
Use verbose mode.
Task 4¶
Download a file.
Task 5¶
Send a POST request.
curl -X POST \
-H "Content-Type: application/json" \
-d '{"message":"Hello"}' \
https://httpbin.org/post
Task 6¶
Retrieve only the HTTP status code.
Task 7¶
Test a REST API endpoint with a custom Authorization header.
Task 8¶
Use curl to test a Kubernetes Ingress or application health endpoint and verify the HTTP response.
Production Troubleshooting¶
Problem:
Check:
↓
Inspect:
- DNS Resolution
- TLS Handshake
- HTTP Status
- Response Headers
- Response Body
↓
Identify:
- Authentication Failure
- Server Error
- Timeout
- Redirect
- Certificate Issue
curl vs wget¶
| curl | wget |
|---|---|
| API Testing | File Download |
| Supports Many Protocols | Optimized for Downloads |
| Sends Custom Requests | Recursive Downloads |
| REST API Automation | Website Mirroring |
| Preferred for HTTP Debugging | Preferred for Bulk Downloads |
Common Mistakes¶
❌ Forgetting quotes around JSON.
✅ Always quote JSON payloads.
❌ Using the wrong HTTP method.
✅ Verify the API documentation.
❌ Ignoring response headers.
✅ Inspect headers during troubleshooting.
❌ Disabling TLS validation in production.
✅ Use valid certificates instead of -k.
❌ Forgetting authentication headers.
✅ Include required credentials or tokens.
Best Practices¶
- Use verbose mode when troubleshooting.
- Check HTTP status codes before parsing responses.
- Protect API tokens and credentials.
- Validate TLS certificates in production.
- Use
-Lwhen redirects are expected. - Format JSON responses using tools like
jqwhen available. - Automate health checks with
curl.
Interview Questions¶
Beginner¶
- What is
curl? - How do you display HTTP headers?
- How do you download a file?
- What does
curl -vdo?
Intermediate¶
- How do you send a POST request?
- How do you authenticate using a Bearer token?
- Explain common HTTP status codes.
- How do you troubleshoot an API using
curl?
Architect Level¶
- Design an API health-check workflow using
curl. - Explain how you would debug intermittent API failures.
- How would you automate production endpoint monitoring with
curl?
Summary¶
In this lesson, you learned:
- The
curlcommand - HTTP and HTTPS Requests
- REST API Testing
- Authentication
- Request Headers
- File Downloads
- HTTP Status Codes
- Enterprise API Troubleshooting
curl is one of the most versatile networking tools available on Linux. It enables engineers to interact with web servers, APIs, cloud services, and Kubernetes applications directly from the command line. Mastering curl is essential for testing APIs, automating HTTP requests, validating service health, and troubleshooting modern web-based infrastructure.
Key Takeaways¶
curlis the standard command-line tool for HTTP and API communication.- It supports GET, POST, PUT, DELETE, and many other request methods.
- Use
-Hto send custom headers and-dto send request data. - Use
-vfor detailed troubleshooting and-Ito inspect response headers. curlis widely used for REST APIs, Kubernetes, cloud services, automation, and monitoring.- Understanding HTTP status codes is essential for diagnosing application issues.
What's Next?¶
In the next lesson, you'll learn about wget.
You'll explore:
- What
wgetis - Downloading Files
- Recursive Downloads
- Resume Interrupted Downloads
- Background Downloads
- Website Mirroring
- Automation with
wget
By the end of the lesson, you'll understand how to efficiently download files, mirror websites, automate file retrieval, and use wget for system administration, DevOps workflows, and software distribution.