Your first requests: GET and downloads

curl · Essentials · 7 min

Fetching a URL from the terminal is the starting point for testing APIs and downloading anything.

In its simplest form, curl https://example.com sends an HTTP GET request and writes the response body to standard output. Response headers are not shown by default.

Saving output:

  • -o file writes the body to a file you name.
  • -O (capital) uses the file name from the URL: curl -O https://example.com/app.tar.gz saves app.tar.gz. Add -J to prefer the name the server suggests in Content-Disposition, and --output-dir dir to choose the directory.

When output goes to a file or pipe, curl shows a progress meter on stderr. -s (silent) hides it, and -sS hides it but still prints errors, the usual choice in scripts.

Redirects are not followed by default: a URL that answers 301 Moved Permanently gives you that short response. Add -L to follow Location headers to the final resource; downloads from GitHub releases and many CDNs need it.

Query strings are part of the URL. Quote URLs containing &, ? or [, otherwise your shell interprets them: curl 'https://api.example.com/search?q=cat&limit=5'.

curl --version shows the version, the TLS library and the protocols and features (HTTP2, HTTP3, ...) your build supports.

Example

curl https://example.com                      # body to stdout
curl -sS -o page.html https://example.com     # save, quiet but show errors
curl -LO https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64
curl -L -o jq --output-dir ~/bin https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-amd64
curl 'https://api.example.com/search?q=cat&limit=5'   # quote the URL!
curl -sS https://api.github.com/repos/curl/curl | head -5
curl --version | head -1                      # curl 8.x (...) libcurl/8.x OpenSSL/3...

Remember

  • curl URL = GET, body to stdout
  • -o name / -O remote name; -L follows redirects
  • -sS: no progress meter, but still show errors

Watch out for

  • Forgetting -L and saving a tiny redirect page instead of the file.

Check yourself

  1. `curl -O https://dl.example.com/latest` saved a 150-byte file instead of the 20 MB release. Most likely fix?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: curl.se/docs/tutorial.html