Skip to content

Using curl to Test APIs

curl is a versatile command-line tool used to send HTTP requests. Developers and system administrators often use it to test APIs, download files, and debug network issues. This guide walks through common tasks with curl.

  • A Linux or macOS system (Ubuntu typically has curl preinstalled).
  • Basic familiarity with HTTP requests.
Terminal window
curl https://api.github.com/users/octocat

This fetches a JSON object containing details for the GitHub user octocat.

Include an authorization header when accessing APIs:

Terminal window
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/data

Send a JSON payload to an API endpoint:

Terminal window
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"test","value":42}' \
https://api.example.com/submit
Terminal window
curl -o response.json https://api.github.com/users/octocat

Combine curl with jq for readable JSON output:

Terminal window
curl https://api.github.com/users/octocat | jq .

Save multiple curl calls in a shell script (test-api.sh):

#!/bin/bash
curl -s https://api.example.com/health | jq .
curl -s -H "Authorization: Bearer $TOKEN" https://api.example.com/data | jq .

Run with:

Terminal window
bash test-api.sh