The curl command transfers data to and from a server using protocols like HTTP and HTTPS. Developers use it to test APIs, check response headers, download files, and debug web requests right from the terminal.
This is part 5 of my Linux networking series. If you missed the earlier posts, start with Linux dig Command: 5 Examples Every Developer Should Know, then read the nslookup, ping, and traceroute posts.
1. Install curl
curl is already installed on many systems. If it's missing, install it on Debian/Ubuntu with:
sudo apt update
sudo apt install curl
Check the version:
curl --version
2. Make a Basic GET Request
curl https://google.com
This sends a GET request and prints the response body to the terminal. Since google.com redirects, you'll see a short HTML page telling you the page has moved. The next examples show how to handle that.
To hide the progress meter in scripts, add -s (silent):
curl -s https://google.com
3. Check Response Headers and Follow Redirects
Show only the response headers with -I:
curl -I https://google.com
This is handy for checking the status code, content type, caching headers, and where a redirect points.
Follow redirects automatically with -L:
curl -L https://google.com
Combine both to see the headers of the final page:
curl -IL https://google.com
4. Download a File
Save the output to a file name of your choice with -o:
curl -o page.html https://google.com
Save the file using its original name from the URL with -O:
curl -O https://example.com/file.zip
Resume an interrupted download with -C -:
curl -C - -O https://example.com/file.zip
5. Send Data to an API (POST Request)
Send JSON using a POST request:
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"name": "Safal", "role": "developer"}'
-
-X POSTsets the HTTP method. -
-Hadds a request header. -
-dsets the request body.
Add an authorization header when an API needs a token:
curl https://api.example.com/data \
-H "Authorization: Bearer YOUR_TOKEN"
Replace YOUR_TOKEN with a real token, and keep real tokens out of your shell history and public posts.
Bonus: Debug with Verbose Output
curl -v https://google.com
The -v option shows the DNS lookup, the connection, the TLS handshake, and the full request and response headers. It's the best way to see what is really happening during a request.
Bonus: Measure How Long a Request Takes
curl -o /dev/null -s -w "Total time: %{time_total}s\n" https://google.com
Here -o /dev/null throws away the body, -s hides the progress meter, and -w prints the timing you ask for. You can also use %{http_code} to print the status code.
Common Options
| Option | What it does |
|---|---|
-s |
Silent mode, no progress meter |
-I |
Show response headers only |
-L |
Follow redirects |
-o |
Save output to a file name you choose |
-O |
Save output using the remote file name |
-X |
Set the HTTP method |
-H |
Add a request header |
-d |
Send data in the request body |
-v |
Verbose output for debugging |
Conclusion
The curl command is one of the most useful tools for developers and DevOps engineers. Used together with dig, nslookup, ping, and traceroute, you can follow a request from DNS lookup all the way to the HTTP response.
Next: Linux ss Command: Simple Examples.
About Me
I'm Safal Bhattarai, a Tech Lead, Full Stack Developer, and DevOps Engineer.
Top comments (0)