Want to test an API (an address on a server that receives and returns data) but don't have a web page to call it from yet? Use curl. It sends an HTTP request from the terminal, following HTTP, the same rules a browser uses. Then it prints the response (the server's answer) exactly as it comes back.
Think of curl as a courier, and your request as the parcel it delivers. You decide the kind of delivery: the method, for example GET to ask for data, POST to send new data and DELETE to remove something. You also decide the label on the parcel (a header, for example "this holds JSON") and what's inside (the body). JSON is a text format for data, such as {"name":"budi"}. The courier comes back with the reply: a status code, the reply's labels (the response headers) and its contents (the response body).
The simplest request: GET
http://localhost:3000/users is an API with a list of users on your own computer (localhost). With no flags at all, curl sends a GET and prints the response body:
$ curl http://localhost:3000/users
[{"id":1,"name":"ani"}]
$ curl -i http://localhost:3000/users
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 24
[{"id":1,"name":"ani"}]
- The first command only shows the body: the list of users in JSON.
-i(include) also shows the status (200 OK, which means success) and the response headers above the body. An empty line separates the headers from the body.
Choosing the method: -X
-X (capital X) sets the method. Without -X, the method is GET. This command sends a DELETE to the same address:
$ curl -i -X DELETE http://localhost:3000/users
HTTP/1.1 405 Method Not Allowed
Allow: GET, POST
Content-Type: application/json
Content-Length: 31
{"error":"Method Not Allowed"}
The server refuses with 405 (that method is not allowed), and the Allow header names the allowed methods: GET and POST. The first digit of a status code shows its kind: 2 means success, 4 means something is wrong with your request, and 5 means the server is the one with the problem.
Sending data: -d and Content-Type
The API at http://localhost:3000/users only accepts JSON. -d (data) fills the body. Guess first: does curl -d '{"name":"budi"}' http://localhost:3000/users create the user budi?
-d also switches the method to POST by itself, so -X POST isn't required. The body is sent exactly as you wrote it. The problem is the Content-Type header, the label for the kind of content in the body. Without -H (the flag that adds headers), curl labels it Content-Type: application/x-www-form-urlencoded, meaning "this holds form data", even when what you wrote is JSON. Here is the result without -H, then with -H:
$ curl -i -d '{"name":"budi"}' http://localhost:3000/users
HTTP/1.1 415 Unsupported Media Type
Content-Type: application/json
Content-Length: 50
{"error":"Content-Type must be application/json"}
$ curl -i -X POST -H "Content-Type: application/json" -d '{"name":"budi"}' http://localhost:3000/users
HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 23
{"id":2,"name":"budi"}
So the first command fails: an API that only wants JSON answers 415 Unsupported Media Type (this kind of content is not supported). The second command is the standard pattern for a JSON POST:
-X POSTstates the method clearly. You can remove it, because-dalready makes it a POST.-H "Content-Type: application/json"adds a header. The format is"Name: value", and-Hcan be repeated for more headers.-d '{"name":"budi"}'is the body. Wrap JSON in single quotes, so the shell doesn't remove the double quotes inside it.- The URL can go before or after the flags. curl takes as the URL the word that isn't a flag and isn't a flag's value (such as the word after
-X).
curl 7.82 and newer also has --json '{"name":"budi"}'. This flag is a shortcut for a POST with two JSON headers: Content-Type, and Accept (the kind of answer you ask for).
Seeing everything: -v
-v (verbose: detailed) shows the request that was really sent and the response that came back. It's the most basic debugging tool (for finding the cause of a problem):
$ curl -v -d '{"name":"budi"}' http://localhost:3000/users
* Trying [::1]:3000...
* Connected to localhost (::1) port 3000
> POST /users HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.5.0
> Accept: */*
> Content-Length: 15
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 415 Unsupported Media Type
< Content-Type: application/json
< Content-Length: 50
<
{"error":"Content-Type must be application/json"}
* Connection #0 to host localhost left intact
- Lines with
>are what curl sent. curl adds all the headers there by itself, including the cause of the problem: thex-www-form-urlencodedlabel. - Lines with
<are what came back from the server. - Lines with
*are curl's own notes about the connection. Their wording differs a little between curl versions.
Other flags you'll use often
| Flag | What it does |
|---|---|
-s |
silent: hides the progress bar, useful in scripts |
-o result.json |
saves the body to a file instead of the screen |
-O |
saves to a file named after the last part of the URL |
-L |
follows HTTP redirects (the server says to go elsewhere, status 301, 302) to the final address |
-I |
sends HEAD: asks for the headers only, no body |
-w "%{http_code}\n" |
prints the status code after the response |
-u budi:secret |
logs in with Basic Auth (a user name and password) |
-H "Authorization: Bearer $TOKEN" |
sends a token (an access code used instead of a password) |
-k |
skips the TLS certificate check (the proof that an https server is real). Dangerous, see ⚠️ below |
A quick check that the server answers: curl -s -o /dev/null -w "%{http_code}\n" http://localhost:3000/users prints just 200.
⚠️ Labels, quotes, methods and -k
- Sending JSON without a Content-Type.
-dalone labels it form data, so a JSON API answers 415 or reads the body as empty. Add-H "Content-Type: application/json". - Wrapping JSON in double quotes. In
-d "{"name":"budi"}", the shell reads the double quotes inside as the end and the start of a quoted text, and throws them away. The server receives{name:budi}. Use single quotes on the outside. - curl in Windows PowerShell. In Windows PowerShell 5.1 (the PowerShell that comes with Windows),
curlis another name forInvoke-WebRequest, a PowerShell command with different flags. Typecurl.exeso the real curl runs (the real curl has come with Windows since Windows 10 version 1803). A second problem: in PowerShell before 7.3, the double quotes inside'{"name":"budi"}'are also removed before they reachcurl.exe, so the server also receives{name:budi}. One fix is to save the JSON in a filebody.jsonand send it with-d @body.json(@means the body is read from that file). Or use PowerShell 7.3 or later (a newer version you install separately), Git Bash or WSL. - Thinking
-X POSTalready sends data.-Xonly changes the method name. There's only a body when there's a-d. -kin production or CI. CI is a server that runs tests automatically.-k(--insecure) turns off the TLS certificate check, so an attacker sitting in the middle can pretend to be your server without being noticed (man-in-the-middle). Use-konly for a local server with a self-made certificate.
TL;DR: curl URL sends a GET request (asking for data) and prints the response body. -X sets the method (the kind of request, such as POST), and -H "Name: value" adds a header. -d fills the body and switches the method to POST (sending data) by itself, when there's no -X. The body goes out as written, but its Content-Type header says form data. So for JSON (a text format for data), add -H "Content-Type: application/json" and wrap the JSON in single quotes. -i shows the response status and headers, and -v shows everything sent (>) and received (<). The URL is the word that isn't a flag and isn't a flag's value (the word after a flag). Don't use -k (skips checking that an https server is real) in production.
⚡ The simplest GET
Write:
curl -i http://localhost:3000/users
Meaning: http://localhost:3000/users is an API (an address that returns data) on your own computer. With no flags, curl uses the method GET (a kind of request that asks for data), then prints the response body (the answer). -i also shows the status (200 OK, success) and the response headers (labels with information) above the body.
⚡ -d without a JSON label
Guess first: -d fills the body (the contents of the request). curl -d '{"name":"budi"}' http://localhost:3000/users goes to an API that only accepts JSON (a text format for data). Is the user budi created?
It fails, 415 Unsupported Media Type. The body is right, but curl labels it as form data in the Content-Type header.
Meaning: -d also switches the method to POST (sending new data) by itself.
⚡ The standard JSON POST
Write:
curl -i -X POST -H "Content-Type: application/json" -d '{"name":"budi"}' http://localhost:3000/users
Meaning: -X sets the method, -H adds a header (here the label "this holds JSON"), and -d holds the body. Wrap the JSON in single quotes, so the shell doesn't remove the double quotes inside.
⚡ See everything
Write:
curl -v -d '{"name":"budi"}' http://localhost:3000/users
Meaning: -v (verbose) shows everything curl sent (lines with >) and what came back from the server (lines with <). In the > lines you can see the form data label (x-www-form-urlencoded) that curl added, though you never wrote it.