ssh — guest: ~ connected
guest@ludovic.toinel.com:~$ man curl

curl — transfer a URL, from the browser

SYNOPSIS

curl [-I] [-i] [-s] [-L] <url>

DESCRIPTION

Requests the URL with the browser's own fetch() and prints the body as text. A JSON body is pretty-printed. A bare host gets https:// (curl example.com fetches https://example.com/). Only http and https are accepted.

At most 65536 bytes (64 KiB) of the body are read and printed; beyond that the transfer is cut and a note says so. Binary content (images, archives, PDF...) is not printed. The request is abandoned after 10 seconds, or at once with Ctrl+C.

OPTIONS

-I, --head headers only (sends a HEAD request) -i, --include response status and headers, then the body -s, --silent no error message and no truncation note -L, --location accepted for compatibility: a browser always follows redirects

Short options combine (curl -sI example.com).

CORS — WHY MANY SITES FAIL

This curl runs inside a web page, so the browser's same-origin policy applies: a page may read the response of another site only when that site explicitly allows it, by answering with an Access-Control-Allow-Origin header (CORS). Most websites do not; public APIs usually do.

When the read is refused, the browser tells the page nothing more than "the request failed" — the same thing it reports for an unknown host, a refused connection or no network. curl therefore cannot say which one happened, and lists the likely causes. The browser's developer console shows the real reason.

Two more browser rules apply:

  • a page served over https may not fetch an http:// URL (mixed content);
  • for another site, only a few response headers are readable (Content-Type, Cache-Control, Content-Language, Expires, Last-Modified, Pragma, sometimes Content-Length) unless the server exposes more with Access-Control-Expose-Headers. The HTTP version is never exposed, so the status line shows no version.

EXAMPLES

curl -I example.com curl https://dns.google/resolve?name=example.com curl -i https://api.github.com/zen curl -s https://api.github.com/zen | wc -c

SEE ALSO

ping, httpstest, httperf, dig, nslookup