DevGuide

How to download a file and test an API with curl

On this page
  1. Download a file
  2. Test an API
  3. Send a POST with a JSON body
  4. The flags you’ll actually reuse

curl is the command-line Swiss Army knife for HTTP, and two jobs cover most days: downloading a file and poking an API. curl -O https://example.com/file.zip saves the file under its own name; add -L so it follows redirects instead of saving a redirect page. To test an API, curl -i https://api.example.com prints the status line and headers along with the body, which is exactly what you want when something returns the wrong thing. It is built into macOS, Linux and Windows 10 and 11. Here's how to download cleanly, how to send a POST with a JSON body, and the handful of flags (-s, -L, -H, -d) you will reach for again and again.

The short answer

curl -O url downloads to a file, curl -L follows redirects, and curl -i url shows the headers with the body for testing an API. Send JSON with -X POST -H and -d. Built into macOS, Linux and Windows.

-Odownload to a file
-ishow headers + body
-Lfollow redirects
Answer card showing curl -O to download a file and curl -i to test an API response.
Two jobs, a handful of flags. The rest of curl is variations on these. PNG

Download a file

Linux
curl -O https://example.com/report.pdf

That saves report.pdf in the current folder. To rename it on the way down use lowercase -o, and if the link redirects (most do), add -L:

Linux
curl -LO https://example.com/file.zip

Test an API

Linux
curl -i https://api.example.com/health

-i prints the status line and headers above the body, so you see the 200 (or the 500) and the content type, not just the JSON. Want headers only? Use capital -I.

Send a POST with a JSON body

Linux
curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' https://api.example.com/items

The -H sets the content type so the server parses it as JSON, and -d carries the body. Drop the -X POST and curl still POSTs, because -d implies it.

Terminal showing curl -O downloading a PDF with a progress bar, then curl -i printing an HTTP 200 response with JSON.
Download on top, API check below. The headers are the half people forget to look at. PNG

The flags you’ll actually reuse

Not many. -O or -o to save, -L to follow redirects, -i or -I for headers, -H to set one, -d to send a body, and -s to silence the progress meter in scripts. To grade the headers a site sends back without memorizing them, our HTTP headers checker does it visually.

Frequently asked questions

What is the difference between curl -O and curl -o?

Capital -O saves the file under the name it has on the server. Lowercase -o lets you choose the name: "curl -o report.pdf https://example.com/r". With neither flag, curl prints the file to your screen, which is rarely what you want for anything binary.

My download saved a tiny HTML file instead of the real one. Why?

The URL redirected and curl saved the redirect page itself. Add -L so curl follows redirects to the final file: "curl -LO https://example.com/file.zip". Most CDN links and shortened URLs need it.

How do I send a POST request with JSON?

Set the header and the body with -H and -d, like "curl -X POST -H header -d data url". curl assumes POST the moment you pass -d, so -X POST is often optional, but writing it makes the intent obvious.

How do I see only the response headers?

Use -I (capital i) for a HEAD request that prints headers only, or -i (lowercase) to print headers plus the body. -I is perfect for checking a status code or a redirect without downloading the whole file.