Mastering `curl` on Linux: Downloads and API Requests
1
min read
.
Updated on
02 Sep 2025
curl is one of the most useful command-line tools for transferring data and testing HTTP APIs. It supports HTTP, HTTPS, FTP, and many other protocols, making it useful for downloads, automation, diagnostics, and API development.
1. Check or Install curl
Check the installed version:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
On Debian or Ubuntu:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
sudo apt update
sudo apt install curl
Other distributions provide curl through their normal package managers.
2. Download Files
Save a remote file using the filename from the URL:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -O https://example.com/file.zip
If the URL redirects, add -L:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -LO https://example.com/file.zip
Choose your own output filename with lowercase -o:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -o custom-name.zip https://example.com/file.zip
Resume a partially downloaded file:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -C - -O https://example.com/file.zip
3. Inspect HTTP Responses
Request only the response headers:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -I https://example.com
For more diagnostic detail about the connection and request, use verbose mode:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -v https://example.com
-d sends a request body and causes curl to use POST unless another method is specified:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -d "param1=value1¶m2=value2" https://example.com/api
You normally do not need -X POST when using -d.
5. Send JSON
Modern curl versions support the convenient --json option:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl --json '{"key":"value"}' https://example.com/api
A widely compatible equivalent is:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -H "Content-Type: application/json" \
-d '{"key":"value"}' \
https://example.com/api
6. Authentication
Basic authentication:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -u username https://example.com/private
curl will prompt for the password, which is preferable to putting a password directly in shell history.
Bearer token authentication:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -H "Authorization: Bearer YOUR_TOKEN" https://example.com/api
Avoid committing real tokens to scripts or repositories. Prefer environment variables or a secret-management system for automation.
7. Cookies and Redirects
Save cookies:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -c cookies.txt https://example.com
Send stored cookies:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -b cookies.txt https://example.com
Follow redirects:
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
curl -L https://example.com
8. Check an HTTP Status Code in a Script
$el.innerText = 'Copy', 2000)"
class="absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity"
>
Copy
#!/usr/bin/env bash
url= "https://example.com"
status_code= $( curl -o /dev/null -sS -w "%{http_code}" " $url" )
printf 'Status code: %s\n' " $status_code"
For scripts that should fail on HTTP errors, consider --fail or --fail-with-body depending on the behavior you need.
Conclusion
curl covers far more than simple downloads. With a small set of options you can inspect HTTP responses, send form or JSON data, authenticate to APIs, manage cookies, follow redirects, and automate health checks from the shell.