How to Test a REST API From the Command Line With curl
When you build or use a web API, you often want to see exactly what it sends and receives, without a browser or a graphical tool in the way. The curl command does that. It is a small, free, open source program that makes HTTP requests from a terminal, and it is installed by default on most Linux and macOS systems, as well as recent versions of Windows.
This guide shows how to use curl for the requests you will meet most often when testing a REST API, and how to read the results. The examples use api.example.com, a placeholder name, so substitute your own server.
A first GET request
The simplest call fetches a resource.
curl https://api.example.com/items
By default, curl performs a GET request and prints the response body. For a JSON API, the output may be a single long line. If you have the jq tool installed, pipe the output through it to format it.
curl -s https://api.example.com/items | jq .
The -s flag stands for silent. It hides the progress meter, so only the response body is printed.
See headers and status codes
Often what matters is the status code or a header, not the body. Two flags help.
-iincludes the response headers in the output.-Isends a HEAD request and shows only the headers.-vshows the full conversation, including the request you sent.
curl -i https://api.example.com/items/42
The first line of the response shows the status, such as 200 OK or 404 Not Found. To print only the code, use a write format.
curl -s -o /dev/null -w "%{http_code}\n" https://api.example.com/items/42
Here -o /dev/null discards the body, and -w prints the code. That is a handy form for scripts and health checks.
Send data with POST
To create a resource, send a POST with a JSON body. Set the method with -X, add a content type header with -H, and give the data with -d.
curl -X POST https://api.example.com/items \
-H "Content-Type: application/json" \
-d '{"name": "notebook", "quantity": 3}'
Always set the Content-Type header, since many servers refuse to parse the body without it. Wrap the JSON in single quotes in Bash, so the double quotes inside are preserved. On Windows, quoting rules differ, so it is often easier to put the JSON in a file and send it with -d @body.json.
Note that using -d already makes curl switch to POST, so -X POST is optional in that case. It is shown here for clarity.
Other methods
REST APIs also use PUT, PATCH and DELETE.
curl -X PUT https://api.example.com/items/42 \
-H "Content-Type: application/json" \
-d '{"name": "notebook", "quantity": 5}'
curl -X PATCH https://api.example.com/items/42 \
-H "Content-Type: application/json" \
-d '{"quantity": 6}'
curl -X DELETE https://api.example.com/items/42
In general, PUT replaces a resource, PATCH changes part of it, and DELETE removes it. Check the documentation of your API, since conventions are not identical everywhere.
Authentication
Many APIs need credentials. Common styles include:
- Bearer token in a header:
-H "Authorization: Bearer $TOKEN". - API key in a header or a query string, depending on the service.
- Basic authentication:
-u username:password.
Store secrets in environment variables rather than typing them into commands, because commands are saved in shell history and may be visible to other users in the process list.
Query strings and special characters
When a URL contains &, the shell treats it as a background operator. Wrap the URL in quotes.
curl "https://api.example.com/items?category=books&limit=10"
You can also let curl build the query string safely with -G and --data-urlencode, which encodes spaces and symbols for you.
curl -G https://api.example.com/search --data-urlencode "q=red shoes"
Following redirects and handling errors
Servers sometimes answer with a redirect. By default curl does not follow it. Add -L to follow the new location. Another useful flag is --fail, which makes curl exit with an error code on HTTP errors such as 404 or 500, rather than printing the error page and returning success. That makes shell scripts behave sensibly.
curl -sS --fail -L https://api.example.com/items
The -sS pair keeps the output quiet but still shows error messages.
Measuring and debugging
-w "%{time_total}\n"prints the total time of the request.--max-time 10stops waiting after ten seconds.-vreveals the TLS handshake, request headers and response headers, which helps find problems with certificates or incorrect headers.--trace-ascii out.txtsaves a complete trace to a file for later reading.-kskips certificate checks. Use it only for local testing, never for real systems, because it removes an important protection.
Saving requests for repeat use
When a call grows long, put it in a script or a small text file of commands you can rerun. Keep tokens out of the file and read them from the environment. For a collection of requests, a plain shell script with one function per endpoint is easy to share and works without extra software.
Frequently Asked Questions
What is the difference between -d and --data-urlencode?
The -d flag sends data exactly as written, which suits JSON. The --data-urlencode flag encodes special characters so they travel safely in form data or a query string.
Why do I get a 415 or 400 error on a POST?
A 415 usually means the server did not accept your content type, so check the Content-Type header. A 400 often means the body is malformed, such as invalid JSON from a quoting mistake. Use -v to see what was actually sent.
Can curl replace a graphical API client?
For testing and scripting, often yes. It lacks a visual interface and saved collections, but it is scriptable, available almost everywhere and good for automated checks.