Table of Contents
The curl command is one of the most powerful and versatile tools for making HTTP requests via the command line. Used by developers, sysadmins, and DevOps engineers, curl supports multiple protocols including HTTP, HTTPS, FTP, and more.
In this guide, you’ll learn how to use curl in terminal, break down curl syntax, explore curl options, and see practical examples that answer popular questions like “How do I run a curl command?” and “What is curl with an example?“.
What is the curl Command and What Does It Do?
curl (short for Client URL) is a command-line tool used to transfer data to or from a server using supported protocols. It’s available by default in most Unix-like systems, making it a standard utility in programming and scripting.
How Do I Run a curl Command?
To run a basic curl command, use your terminal or bash shell like this:
curl https://example.com
This simple command fetches the HTML content of https://example.com. It’s particularly helpful for testing APIs, downloading files, or communicating between services.
What is curl with an Example?
Here’s a quick real-world example of using curl to send a POST request with JSON data to an API:
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "securepass"}'
This sends a JSON payload to authenticate a user. Now let’s dive into more practical uses.
9 Must-Know curl Command Examples (With Syntax Explained)
Use Case | Command | Explanation |
---|---|---|
1. Fetch a Website | curl https://example.com | Downloads the raw HTML content of a webpage. |
2. Save Output to File | curl -o file.html https://example.com | Saves the response data into file.html. |
3. Follow Redirects | curl -L https://oldurl.com | Follows HTTP 301/302 redirects automatically. |
4. Use Custom Headers | curl -H “API-Key: 12345” https://api.example.com | Adds a custom header to your request for authentication or tracking. |
5. Send JSON via POST | curl -X POST -H “Content-Type: application/json” -d ‘{“name”: “John”}’ https://api.com | Posts JSON payloads to an API endpoint. |
6. Basic Authentication | curl -u user:pass https://secure.example.com | Sends credentials for HTTP authentication. |
7. Upload a File | curl -F “[email protected]” https://upload.com | Uploads a file via multi-part POST request. |
8. Check HTTP Headers | curl -I https://example.com | Returns HTTP response headers only. |
9. Silent Mode | curl -s https://example.com | Suppresses the progress meter for cleaner output. |
What is the curl type command?
The term curl type command typically refers to common uses or ‘types’ of requests you make using curl — such as GET, POST, PUT, DELETE — especially in web API testing. For example:
curl -X PUT https://api.example.com/users/1 -d '{"email": "[email protected]"}'
This updates a user’s email using the PUT method.
Popular curl Command Line Options Explained
Here’s a quick list of commonly used curl command line options to help you master its versatility:
- -X: Specify request type (GET, POST, PUT, DELETE)
- -H: Add headers
- -d: Provide data in body
- -u: Username:password authentication
- -F: Multi-part form uploads
- -I: Show HTTP headers
- -o: Output file
- -s: Silent mode
- -L: Follow redirects
Where is curl Available?
You’ll find curl in Linux distributions by default. It’s pre-installed on most Unix systems, and also available on Mac and Windows. You can also use curl in shell scripts, cron jobs, and bash pipelines.
FAQs About curl Command
Is curl pre-installed on Linux?
Yes, most Linux distributions like Ubuntu and CentOS come with curl pre-installed. You can check by running curl --version
.
What is the difference between curl and wget?
While both are used for downloading files, wget is best for recursive downloads, whereas curl is ideal for API requests and scripting.
Can I use curl in bash scripts?
Absolutely. curl bash usage is common in DevOps. Combine curl with if statements, piping, and loops to automate workflows.
How do I view curl’s help menu?
Run curl --help
in your terminal to see a complete list of curl command options.
Is curl secure?
Yes, as long as you use HTTPS and secure headers. Avoid exposing passwords in scripts without proper encryption.
Conclusion: Mastering curl for the Real World
Whether you’re troubleshooting APIs, uploading files, or automating scripts, the curl command is an indispensable tool. Mastering curl syntax and curl options not only boosts productivity but also strengthens your command-line and DevOps skillset. Start experimenting today with the examples provided, and dive into more advanced use cases as you grow.