How to Use cURL to Test REST APIs from the Linux Command Line
cURL is one of the quickest ways to inspect and test a REST API without installing a graphical client. It is available on most RHEL, CentOS, Fedora, and other Linux systems, making it useful for developers, helpdesk staff, and administrators working directly on a server. Learn more about H4h Itea2.org.
A cURL request can show whether an endpoint is reachable, whether authentication works, and whether a service returns valid JSON. The same commands also fit neatly into shell scripts, monitoring checks, and deployment workflows for applications hosted in Sydney, Melbourne, Brisbane, or elsewhere.
Check cURL Installation
Verify that cURL is installed before testing an endpoint:
curl --version
On Fedora, install it with:
sudo dnf install curl
Older CentOS systems may use:
sudo yum install curl
The output displays the cURL version, supported protocols, and SSL libraries. HTTPS support is essential for production APIs, particularly when sending customer or payment data across Australian networks.
A simple request confirms that DNS, routing, and TLS are working:
curl https://api.example.com/health
If the server returns JSON, cURL prints it directly to the terminal. A response such as {"status":"ok"} confirms that the endpoint answered, but it does not necessarily prove that every application dependency is healthy.
Send GET Requests and Read Responses
GET is commonly used to retrieve resources. Add -i to display response headers alongside the response body:
curl -i https://api.example.com/users
Important headers include HTTP/2 200, Content-Type: application/json, cache directives, and rate-limit information. A 404 usually indicates an incorrect path, while 401 or 403 points to missing or insufficient credentials.
Query parameters should be URL-encoded when they contain spaces or special characters:
curl -G 'https://api.example.com/orders' \
--data-urlencode 'city=Melbourne' \
--data-urlencode 'currency=AUD'
Testing Australian-specific values is useful for local applications. An online retailer may return prices in Australian dollars, include GST, and apply different delivery rules for Perth than for Sydney. cURL makes these variations easy to test repeatedly.
Add Headers and Authentication
HTTP headers communicate the requested format and authentication details. A typical JSON request looks like this:
curl https://api.example.com/profile \
-H 'Accept: application/json' \
-H 'Authorization: Bearer YOUR_TOKEN'
Do not place real tokens in shell history or public scripts. For temporary work, read the token from an environment variable:
export API_TOKEN='replace-with-a-secret'
curl https://api.example.com/profile \
-H "Authorization: Bearer $API_TOKEN"
API keys may use a custom header instead:
curl https://api.example.com/reports \
-H 'X-API-Key: YOUR_API_KEY'
For systems handling Australian customer information, review the Privacy Act 1988 and its Australian Privacy Principles. Avoid copying names, addresses, health details, or access tokens into shared terminals and support tickets.
Test POST, PUT, and DELETE Operations
Use -X POST with -d to submit JSON data:
curl -X POST https://api.example.com/users \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{"name":"Alex Taylor","email":"alex@example.com"}'
The Content-Type header tells the server how to parse the body. For larger payloads, store JSON in a file and use the @ prefix:
curl -X PUT https://api.example.com/users/42 \
-H 'Content-Type: application/json' \
--data @user.json
A DELETE request can remove a resource:
curl -X DELETE https://api.example.com/users/42
Use a test account or staging environment for destructive methods. When an API sits behind Nginx configuration guides, check that the reverse proxy passes request methods, headers, and body data correctly.
Inspect Errors and Connection Problems
The -sS options suppress the progress meter while still showing network errors:
curl -sS -i https://api.example.com/health
For detailed TLS, DNS, redirect, and request information, use verbose mode:
curl -v https://api.example.com/health
Avoid sharing verbose output publicly if it contains cookies or authorisation headers. To follow HTTP redirects, add -L:
curl -L https://api.example.com/old-endpoint
Set a timeout so a monitoring script does not hang indefinitely:
curl --connect-timeout 5 --max-time 15 \
-o /tmp/api-response.json \
-w '%{http_code}\n' \
https://api.example.com/health
The -w option prints selected metrics after the request. This is useful for cron jobs operating on Australian Eastern Standard Time or Australian Eastern Daylight Time, where scheduled checks should be planned carefully around daylight-saving changes.
Automate Checks Safely
Shell scripts can test both the HTTP status and the response body. The --fail-with-body option makes many HTTP errors visible to automation:
if curl --fail-with-body --silent --show-error \
--max-time 10 https://api.example.com/health >/tmp/health.json; then
echo "API is available"
else
echo "API check failed" >&2
exit 1
fi
For readable JSON, pipe the response through jq when it is installed:
curl -s https://api.example.com/health | jq .
A useful API test checks status code, content type, response time, and expected fields. Keep credentials in protected environment files or a secrets manager, use HTTPS, and remove temporary response files containing personal data. A practical starting point is to run one authenticated GET request with -i, then add JSON POST tests and timeouts as the service grows.