curl for Beginners: Test Any API From Your Terminal
Your team's app says "Something went wrong" when it saves an order, and nobody knows whether the network, the server or the app is to blame. Sending the same request by hand and reading the raw reply settles it. This lesson is curl for beginners: you fetch pages, read status codes and headers, send data to an API (a service that programs call over HTTP), and use curl's errors to find the layer that broke.
Your first requests: plain, -i, -I and -v
curl is a command-line program that sends a request to a URL and prints what comes back. It speaks HTTP, HTTPS and many other protocols, and comes with macOS, Linux and Windows 10 or later. You met it in lesson 6; each command below asks example.com for its home page:
curl https://example.com
curl -i https://example.com
curl -I https://example.com
curl -v https://example.com
With no options, curl sends a GET and prints only the body: the HTML of the Example Domain page. -i (long name --show-headers) adds the status line and response headers above the body. -I (--head) sends a HEAD request, which asks for the headers alone, so no body comes back.
-v (--verbose) prints every step curl takes. Each line has a prefix: * marks curl's own notes, > what it sent and < what it received. Read top to bottom, the lines replay the journey from lesson 10: DNS, TCP, TLS, then the HTTP request and response.
Notice that curl reports Connected only after TLS has checked the certificate. Other curl versions word or order a few lines differently, but the prefixes and the steps stay the same.
Useful flags: save, follow, give up, print the status
Four more flags cover most daily work. httpbin.org is a free test service whose paths behave in known ways and answer in JSON (JavaScript Object Notation), a text format of names and values in curly braces, such as {"a":1}:
curl -o page.html https://example.com
curl -L https://httpbin.org/redirect/1
curl --max-time 2 https://httpbin.org/delay/5
curl -s -o /dev/null -w '%{http_code}\n' https://example.com
-o page.html saves the body to a file instead of printing it, and curl shows a progress meter, a small table of transfer figures. -s (silent) hides it, but also hides error messages, so drop -s when something fails.
A redirect is a 3xx status with a location header naming a new address (lesson 6). /redirect/1 answers 302 with a location header pointing to /get. Plain curl stops there; -L (--location) follows it and prints the JSON from /get.
By default curl sets no limit on a whole transfer, so a stuck server can keep it waiting. --max-time 2 gives up after 2 seconds. /delay/5 waits 5 seconds before answering, so you get curl: (28) Operation timed out after 2002 milliseconds with 0 bytes received; the milliseconds vary.
-w (write-out) prints details after the transfer: %{http_code} is the status code and \n a new line. /dev/null is a special file that discards whatever is written to it, so the last command prints just 200. curl's manual describes every flag.
-H and -d come next.Sending data: a POST with a JSON body
To create or change something, an API usually expects a POST with a body, often in JSON. httpbin.org/post echoes back what it receives:
curl -X POST -H 'Content-Type: application/json' -d '{"a":1}' https://httpbin.org/post
-X POST sets the method, -H adds a request header (Content-Type says the body is JSON) and -d puts the data in the body. The single quotes make your shell, the program that reads what you type, pass the text exactly as written; without them it strips the double quotes. Windows Command Prompt does not treat single quotes this way, so use the lab shell.
-d already makes the request a POST, so -X POST is optional, and with -L it forces POST onto every redirect. Leave out the header, though, and curl labels the body application/x-www-form-urlencoded, the format of HTML forms, which a JSON API may reject or misread.
httpbin answers with JSON about your request, trimmed here:
{
"data": "{\"a\":1}",
"headers": {
"Content-Type": "application/json"
},
"json": {
"a": 1
},
"url": "https://httpbin.org/post"
}
data is the body as sent; json is the same body parsed, proof that the server read it as JSON. Since curl 7.82.0, --json '{"a":1}' does all this in one flag: a POST with Content-Type and Accept set to application/json.
Reading the result: status, headers, body and timing
Every reply has lesson 6's three parts. Read the status code first: 2xx worked, 3xx points elsewhere, 4xx means a wrong request and 5xx a failed server. Then the headers, such as content-type for what the body is, then the body itself.
One surprise: curl's exit code, the number a command hands back to the shell when it ends, is 0 (success) even for a 404 or a 500, because the transfer worked. Scripts that must notice add -f (--fail): curl then exits with 22 for any status of 400 or above.
For timing, -w can print when each step finished, in seconds from the start, so subtract neighbouring lines to get one step's cost. The figures are illustrative:
$ curl -s -o /dev/null -w 'dns %{time_namelookup}\nconnect %{time_connect}\ntls %{time_appconnect}\nfirst byte %{time_starttransfer}\n' https://example.com
dns 0.012034
connect 0.024710
tls 0.051388
first byte 0.075902
Here DNS took about 12 ms, TCP 13 ms more and TLS 27 ms, and the first byte came 25 ms later. A first-byte gap much bigger than the connect step points at a slow server, not the network.
Using curl to debug: each layer fails at its own step
Because curl works through the steps in order, where it stops tells you which layer to check:
curl: (6) Could not resolve host means DNS gave no address: check the name with dig (lesson 20). Code 7, Failed to connect, at once usually means the host answered that nothing listens on that port; code 28 while connecting means no answer at all, usually a firewall dropping packets (lesson 13).
Code 60, SSL certificate problem, or 35, SSL connect error, means TCP worked but TLS did not; the words after the code give the reason (lesson 18). A 4xx or 5xx status means DNS, TCP and TLS all worked, so look at the application. Operation timed out after the Connected line means the server accepted you but answered too slowly.
Check yourself
curl -v --max-time 10 to a new server prints a Trying line, then curl: (28) Connection timed out. Is DNS the problem?
No. The Trying line shows curl already had an address, so DNS worked; no reply while connecting points at a firewall dropping packets or a broken path.
A script runs curl -s against an API and checks that curl succeeded. The API starts returning 500, yet the script reports success. Why?
curl exits with 0 whenever the transfer works, whatever the status code. Adding -f makes it exit with 22 for any status of 400 or above.
You send -d '{"a":1}' to a JSON API without a -H option, and the API treats your data as a form. What went wrong?
Without the header, curl labels a -d body application/x-www-form-urlencoded, so the server parsed it as form data. Add -H 'Content-Type: application/json', or use --json.