Debugging an API request with cURL is easier when you separate request construction from response diagnosis. This guide shows how to build repeatable GET and POST requests, send headers and JSON, inspect status codes and response headers, follow redirects, investigate TLS and authentication failures, and compare cURL with browser-based tools when choosing a testing workflow.
Overview
cURL is a command-line HTTP client available on many development systems. It is useful for API request testing because a request can be copied into documentation, committed to a script, run in CI, or compared across environments. Unlike a browser address bar, cURL also gives you direct control over HTTP methods, headers, request bodies, redirects, certificates, and output.
A reliable debugging workflow has four stages:
- Construct the smallest request that should work.
- Make the response visible, including its status and headers.
- Add authentication, content negotiation, and other options one at a time.
- Compare the failing request with a known-good request or environment.
Start with a safe endpoint or test environment. Avoid placing real access tokens in shell history, shared terminals, screenshots, or committed scripts.
How to compare options
The best debugging method depends on what you need to observe. cURL is strong when you want repeatability, exact command-line control, and a request that can be automated. Browser DevTools is often faster for inspecting a request made by a frontend application, including cookies, CORS behavior, timing, and the request that the browser actually sent. An API client can be convenient for saved environments and readable request collections, while a small test script is better when you need assertions, fixtures, or multi-step flows.
Compare tools using these questions:
- Can you reproduce the request? Record the method, URL, query parameters, headers, and body.
- Can you see the complete response? Check status, headers, body, redirects, and timing where relevant.
- Can you isolate variables? Change one header, credential, or payload field at a time.
- Can you run it in another environment? A command should be understandable on a developer machine and in CI.
- Can you protect secrets? Prefer environment variables or a secure secret store over literal credentials.
For a frontend-specific investigation, pair cURL with the Browser DevTools Network Tab guide. For repeatable checks, consider the patterns in the cURL API Testing guide.
Feature-by-feature breakdown
Build a basic GET request
Use -i to include response headers in the output:
curl -i https://api.example.test/v1/usersTo separate headers from the response body, use -D with a file:
curl -D response-headers.txt -o response-body.json https://api.example.test/v1/usersThis makes it easier to inspect the server response without mixing diagnostic information into JSON.
Send query parameters safely
For parameters containing spaces, ampersands, or other reserved characters, let cURL encode them:
curl -G "https://api.example.test/v1/search" \
--data-urlencode "q=error logs" \
--data-urlencode "limit=20"The -G option places data arguments in the query string instead of sending them as a request body.
Build a POST request with JSON
A common cURL POST JSON example includes both the content type and the body:
curl -i -X POST "https://api.example.test/v1/users" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
--data '{"name":"Ada","role":"admin"}'Content-Type describes the format you are sending. Accept describes the response format you prefer. If the server reports malformed JSON, validate punctuation, quoting, and required fields before changing other options.
Send authentication without exposing it in the command
Use an environment variable rather than pasting a token directly into a command:
export API_TOKEN='replace-with-a-test-token'
curl -i "https://api.example.test/v1/profile" \
-H "Authorization: Bearer ${API_TOKEN}"Shell behavior varies, so check that the variable is set and avoid printing it. Basic authentication can be tested with:
curl -i -u "${API_USER}:${API_PASSWORD}" https://api.example.test/v1/profileDo not treat a successful token decode as proof that a token is valid. A JWT decoder can help you read claims, but signature verification and server-side authorization are separate checks. See the JWT Decoder guide for safe claim inspection.
Inspect redirects, TLS, and transport details
Use -L to follow HTTP redirects:
curl -i -L https://api.example.test/old-endpointReview each redirect carefully, especially when a request changes host or involves credentials. For verbose connection details, use:
curl -v https://api.example.test/v1/healthVerbose output can reveal DNS resolution, connection setup, TLS negotiation, sent headers, and received headers. Do not share verbose logs without removing authorization values, cookies, and other sensitive data. Avoid disabling certificate verification as a routine fix. If TLS validation fails, check the hostname, certificate chain, system clock, trust store, proxy, and server configuration first.
Interpret HTTP status codes
| Status | Typical direction for investigation |
|---|---|
| 2xx | The server accepted or completed the request; inspect the body and returned data. |
| 3xx | Inspect the Location header and decide whether redirects should be followed. |
| 400 | Check JSON syntax, field names, query parameters, and required values. |
| 401 | Check whether credentials are present, correctly formatted, unexpired, and intended for this API. |
| 403 | Check permissions, scopes, roles, policy rules, IP restrictions, and environment differences. |
| 404 | Verify the path, API version, host, method, and whether the resource exists in this environment. |
| 405 | Confirm that the endpoint supports the method being sent. |
| 409 | Investigate state conflicts such as duplicate resources or an invalid transition. |
| 429 | Check rate-limit headers and retry guidance; avoid uncontrolled retry loops. |
| 5xx | Separate a server failure from a malformed request by comparing with a known-good request and checking server logs. |
To make a command fail in scripts when the HTTP response indicates an error, use --fail-with-body where supported by the installed cURL version:
curl --fail-with-body -sS -i https://api.example.test/v1/healthThe -sS combination suppresses the progress meter while preserving errors. Remember that HTTP status handling and network-level failures are different concerns; capture both in automated checks.
Best fit by scenario
- Quick endpoint check: Start with
curl -i, then add headers or a body only when the endpoint requires them. - Authentication failure: Compare an unauthenticated request, a request with the expected authorization scheme, and a known-good token. Check the exact host and environment.
- Frontend-versus-API mismatch: Use browser DevTools to copy the browser request, then reproduce it with cURL. Compare cookies, origin-related headers, payload serialization, and redirects.
- Unexpected JSON: Save headers and body separately, confirm the response content type, and inspect whether a proxy or redirect returned HTML instead.
- CI or regression testing: Store a minimal command or script with injected secrets, explicit timeouts, and a clear failure condition. Keep test data safe to rerun.
- Shell quoting problems: On Unix-like shells, single quotes preserve JSON double quotes. In PowerShell and Windows command shells, quoting and line continuation differ; test the command in the target shell rather than copying syntax blindly.
When to revisit
Revisit this workflow when an API changes its authentication scheme, required fields, media types, redirect behavior, rate limits, or error format. It is also worth reviewing after upgrading cURL, changing operating systems or shells, moving behind a proxy, or comparing local, staging, and production environments.
Keep a small, sanitized request fixture for each important endpoint. Record the expected method, URL pattern, required headers, sample body, status code, and safe response checks. When a request fails, rerun the smallest fixture first, enable -v only as needed, and add one option at a time. This turns cURL from a one-off troubleshooting command into a dependable developer workflow tool. For the wider release process, connect API checks with the GitHub Actions CI/CD guide and verify deployment-related routing with the website deployment checklist.