TL;DR

curl is the first tool for API debugging. Essentials: curl -sS URL, curl -X POST -H 'Content-Type: application/json' -d '{}' URL, curl -w '%{http_code}'.

Basic Requests

NeedCommand
GET with headers in outputcurl -i URL
Silent modecurl -sS URL
Headers onlycurl -I URL
Follow redirectscurl -L URL
Save to filecurl -o file.txt URL / -O

Sending Data

NeedCommand
POST JSONcurl -X POST -H 'Content-Type: application/json' -d '{"k":"v"}' URL
Form datacurl -d 'k=v&k2=v2' URL
Upload filecurl -F 'file=@./a.txt' URL
Custom headercurl -H 'Authorization: Bearer TOKEN' URL
Cookiescurl -b 'name=value' URL

Useful Flags

FlagPurpose
-w '%{http_code}\n'Print status code only
--max-time 10Timeout (seconds)
--connect-timeout 5Connect timeout
-kSkip TLS verification (debug only)
-x http://host:portUse proxy
-A 'UA'Custom User-Agent

Debugging Notes

FAQ

Why does curl return 000?

000 means the connection failed or timed out — it is not an HTTP status code. Run curl -v URL to see where it stops, then add --connect-timeout 5 and --max-time 10; use -k only for debugging.

How do I send a POST request with JSON?

curl -X POST -H 'Content-Type: application/json' -d '{"k":"v"}' URL. Without the JSON content type, -d sends form-encoded data.

How do I download a file with curl?

Use curl -o file.txt URL to set the local filename, or curl -O URL to keep the remote name. Add -L to follow redirects and --max-time for large downloads.

How do I print only the HTTP status code?

curl -s -o /dev/null -w '%{http_code}' URL — the standard pattern for health checks.

How do I fix "SSL certificate problem" errors?

Update the system CA store (apt install ca-certificates or the OS update) instead of blindly using -k. Use --insecure only for debugging.

Sources

最后更新:2026-08-04