How to Use cURL for API Testing: The Beginner's Guide

CodexFuse Team · 2026-11-04T02:10:57.976Z · 11 min read min read

Learning how to use cURL for API testing is the fastest way to stop guessing what an API is doing and start seeing it. cURL sends one HTTP request from your terminal and prints exactly what comes back — no account, no app, no setup. This beginner's guide walks you through the whole arc: your first GET request, reading status codes and headers, POSTing JSON, authenticating with Basic auth and Bearer , and the debugging flags that explain a failing call. Every command below was run and verified on a real HTTP server for this guide in November 2026.

If you would rather click through requests in a browser first, our REST API tester gives you the same request/response loop with a visual interface. And if the word "API" itself is still fuzzy, read what is an API before this page — that article covers the concept; this one is the hands-on practice.

What Is cURL, Exactly?

cURL is a command-line tool for transferring data from or to a server using URLs — that is the official man page's own one-line definition, and for API testing it is the whole story. You type a request, curl sends it over HTTP or HTTPS, and the server's response lands in your terminal. The man page adds one behavior every beginner should know: unless you tell it otherwise, curl writes the received data to stdout, which is why a plain curl https://api.example.com/users just prints the response body and exits.

Check that you are ready with one command: curl --version. The machine this guide was written on answered curl 8.5.0; the curl project's own release table lists 8.23.0 (October 14, 2026) as current, with the next release planned for December 9, 2026. Every flag in this guide works on any recent curl, so an older banner is no reason to upgrade before you start practicing.

How Do You Test an API with cURL?

You test an API with cURL by building one request at a time, starting simple and layering on complexity. This is how to test an API with cURL end to end: the five-step arc below takes a complete beginner from a first GET to a debugged, authenticated, saved request — it is the shape every endpoint test follows, and it doubles as the snippet to keep beside your terminal:

  1. Send your first GET. curl https://httpbin.org/get — a public echo API that returns what you sent. GET is the default method; no flags needed.
  2. Read the status line and headers. curl -i https://httpbin.org/get — the -i flag prints the HTTP status and response headers above the body, which is the first thing to check when a response looks wrong.
  3. POST JSON. curl -X POST -H "Content-Type: application/json" -d '{"name":"ada"}' https://httpbin.org/post — method flag, content-type header, payload. The echo API returns your JSON parsed, proving the server received it.
  4. Authenticate. curl -u ada:secret https://httpbin.org/basic-auth/ada/secret for Basic auth, or -H "Authorization: Bearer <redacted>" for token APIs. Both were verified against a live server for this guide.
  5. Debug and save. curl -v shows the full request/response exchange when something fails; -o response.json saves a big response to a file; --max-time 10 stops curl hanging on a dead server.

How Do You Send a POST Request with JSON in cURL?

The curl post request json pattern never changes: the method flag, a content-type header, and the payload. The canonical shape is curl -X POST -H "Content-Type: application/json" -d '{"name":"ada"}' https://api.example.com/users, and the server in this guide's verification run received the JSON parsed and the content-type intact. Two quoting facts save beginners hours: on macOS and Linux shells, wrap the whole payload in single quotes so the shell passes the double quotes through untouched; always quote the placeholder <redacted> too, because an unquoted < is a shell redirect — this guide's own test run proved it the hard way.

Three conveniences follow from the man page and this guide's tests. First, -d implies POST, so curl -d '{"a":1}' URL sends a POST even without -X — verified on a live server. Second, -d @payload.json reads the payload from a file, which ends quoting pain for large bodies. Third, if you would rather assemble commands in a UI and copy the result, our curl command builder generates them from a form — a different job from this guide, which teaches the hand-written form.

How Do You Authenticate a Request with cURL?

Most real APIs need credentials, and the two patterns you will meet first are Basic auth and token auth. For HTTP Basic auth, curl -u username:password https://api.example.com/admin makes curl build the base64 Authorization header for you — this guide's echo server confirmed the credentials arrived valid. For a curl authentication Bearer <redacted> flow, send the token as a header yourself: curl -H "Authorization: Bearer <redacted>" https://api.example.com/me. The long form --oauth2-bearer "abc123" sends exactly the same header and exists for scripts that want the intent spelled out.

Treat tokens like passwords, because your shell remembers them: never paste a real token into a tutorial command, never share a command containing one, and prefer reading it from an environment variable — curl -H "Authorization: Bearer $API_TOKEN" URL — so the secret never lands in your shell history. The placeholder <redacted> in every example on this page is deliberate.

What Are the Most Useful cURL Commands for API Testing?

These curl API testing commands cover roughly ninety percent of endpoint testing. Each row below was run against a real HTTP server for this guide; the "verified" column says what actually happened, not what the documentation promises:

CommandWhat it doesVerified behavior
curl URLSimple GET, prints the bodyServer received GET, body printed to stdout
curl -i URLIncludes status line + response headersHeaders printed above the body
curl -I URLHEAD request: headers only, no bodyHeaders only, zero body bytes
curl -X POST -H "Content-Type: application/json" -d '{...}' URLSends a JSON bodyServer parsed the JSON, content-type intact
curl -X PUT -d "v=1" URL / -X DELETE URLUpdate / delete a resourceServer saw PUT and DELETE methods
curl -u user:pass URLHTTP Basic authCredentials arrived valid
curl -H "Authorization: Bearer <redacted>" URLToken authServer received the Bearer
curl -L URLFollows redirects302 followed to a final 200
curl -v URLVerbose: full request/response exchangeSent (>), received (<), and connection (*) lines shown
curl -sS --max-time 10 URLSilent, but still shows errors; caps the whole run at 10 secondsConnection failure reported, no progress noise
curl -s -o out.json -w "code=%{http_code} time=%{time_total}s" URLSaves the body, prints a one-line summaryValid JSON file saved, summary printed
curl -G --data-urlencode "q=hello world" URLGET with URL-encoded query paramsServer saw /search?q=hello+world

One more man-page fact explains a common confusion: curl disables its progress meter automatically when it is writing data to your terminal, so the -s (silent) flag only matters when you combine it with output like -o. And -k (skip TLS verification) exists for local development servers with self-signed certificates — never use it against production, because it disables the check that proves you are talking to the real server.

cURL vs Postman for API Testing: Which Should You Learn First?

Learn cURL first if your goal is speed and scriptability: it is already installed, it runs anywhere, and a one-line command tests an endpoint in under a second. GUI tools win when requests get complex — saved collections, shared workspaces, test scripts, and visual response viewers. The honest 2026 answer is that working developers use both: cURL for the quick check and the CI script, a GUI client for the exploratory session.

For the GUI lane, our Thunder Client beginner guide covers a popular VS Code-based client that keeps requests next to your code. Postman itself remains the heavyweight GUI choice. Neither replaces the other lane — and once a GUI client has the perfect request, the usual next step is exporting it as a cURL command for a script, which is exactly where this guide's flags pay off.

How Do You Debug a Failing API Call with cURL?

Debug a failing call by making the invisible visible, in this order. Start with curl -i to see the status code: a 401 means your auth is wrong, a 404 means the path is wrong, a 400 usually means the body is malformed. Then escalate to curl -v and read the trace: lines starting with > are headers curl sent, lines starting with < are headers the server returned, and * lines narrate the connection — this is where you spot a missing Authorization header or a redirect you did not expect.

For the stubborn cases, three more flags close the loop. Add -L when a 301/302 chain is swallowing your request — this guide watched a redirect resolve to a final 200 with it. Cap hangs with --max-time 10 so a dead server fails fast instead of hanging your terminal. And save the evidence with -D headers.txt, which dumps the response headers to a file. When the command finally works, our curl to code converter turns it into equivalent fetch or Python requests code — a different job from testing, which is why it lives on a separate tool page.

7 Mistakes Beginners Make with cURL

  1. Unquoted JSON. -d {"a":1} lets the shell eat your quotes. Single-quote the whole payload: -d '{"a":1}'.
  2. Forgetting the content-type header. Without -H "Content-Type: application/json", most APIs reject or misread your body.
  3. Not knowing -d implies POST. If your GET suddenly becomes a POST, a leftover -d is the culprit — verified on a live server.
  4. Skipping -L. curl does not follow redirects by default; a 301 response is not an error in your request, it is a missing flag.
  5. Mixing up -i and -I. Lowercase -i includes headers with the body; uppercase -I fetches headers only.
  6. Pasting real tokens. They land in shell history. Use $API_TOKEN and keep <redacted> in every shared example.
  7. Using -k against production. It disables certificate verification — fine for localhost with a self-signed cert, never for a live API.

Sources

Every fact in this guide was verified against a named source fetched on 2026-11-04 (US Pacific).

  • curl.se/docs — the official Documentation Overview, including the free "Everything curl" book (fetched 2026-11-04).
  • curl.se/docs/manpage.html — the official curl man page: "curl is a tool for transferring data from or to a server using URLs," stdout-by-default behavior, progress-meter rules, and the -i/-I/-H/-X/-d/-u/-v/-L/-s/-w/--max-time/-o/-G/-D/--oauth2-bearer flag definitions (fetched 2026-11-04; the page documents curl 8.23.1).
  • curl.se/docs/releases.html — the official release table: 8.23.0 shipped October 14, 2026; 8.22.0 shipped September 2, 2026; next release planned December 9, 2026 (fetched 2026-11-04).
  • Live terminal verification runs on this guide's machine (curl 8.5.0) against a local HTTP echo server: 20 commands covering GET, -i, -I, POST JSON, PUT, DELETE, -u, Bearer , --oauth2-bearer, -L, -v, -s/-S, -w, --max-time, -o, -D, -G with --data-urlencode, -d implying POST, and -d @file (run 2026-11-04).
  • DEV Community, "How to Use curl for API Testing" (224 days old) and DevToolLab, "How to Use curl: 12 Commands for APIs" (38 days old) — the current independent competition found in the intent check (search evidence, 2026-11-04).

Frequently Asked Questions

Do I need to install cURL?

Run curl --version first — on most machines that is the entire installation step, because cURL ships with the operating system. If your system somehow lacks it, your OS package manager carries it. The command prints a version banner plus the protocols and libraries your build supports.

What is the difference between curl -i and -I?

Lowercase -i (include) runs a normal request and prints the response headers above the body — use it when a response looks wrong. Uppercase -I (head) sends a HEAD request and prints only the headers, with no body — use it for a quick "is this endpoint alive" check without downloading anything.

How do I test an API that needs a login?

Use -u username:password for HTTP Basic auth and -H "Authorization: Bearer <redacted>" for token-based APIs — both patterns are covered with verified examples in the authentication section above. Read the token from an environment variable instead of pasting it, so it never lands in your shell history.

Is cURL better than Postman for API testing?

Neither is better in general. cURL wins for one-off checks, scripting, and servers without a GUI; Postman and VS Code clients like Thunder Client win for saved collections, team sharing, and visual exploration. Working developers routinely use both — see the cURL vs Postman section above for the full comparison.

Why does my JSON POST return a 400 error?

The usual suspects, in order: unquoted JSON that the shell mangled, a missing Content-Type: application/json header, or a payload that is not valid JSON at all. Re-run the same command with -v to see exactly what curl sent — the trace almost always names the culprit.

Featured on Twelve Tools Hunt for Tools Featured on LaunchBuff Launch Llama Tools Launch your MCP Top Web Development Companies in the US - OnToplist.com Designer Listings Listed on SeeWhatNewAI