Basic GET and POST Requests
curl https://api.example.com/users performs a GET request. Add -X POST -d '{name":"John"}' for POST with JSON body. Always include -H "Content-Type: application/json" when sending JSON. These three flags — -X, -d, -H — cover 80% of API testing.
Headers and Authentication
Set headers with -H "Authorization: Bearer token123". For Basic auth, use -u username:password. Multiple headers: repeat -H for each. To see response headers, add -i (include headers) or -I (headers only, HEAD request).
Debugging with Verbose Mode
curl -v shows the full HTTP conversation — DNS resolution, TLS handshake, request headers, response headers, and body. For even more detail, --trace-ascii - dumps raw bytes. When debugging API issues, -v is your first tool before reaching for more complex solutions like Postman or Insomnia.
JSON Formatting with jq
Pipe cURL output to jq for pretty-printing: curl -s api.example.com/users | jq.. Filter specific fields: jq '.data[] |.name'. The -s (silent) flag suppresses progress bars for clean piping. This combination replaces most GUI API testing for simple queries.
File Uploads and Downloads
Upload a file: curl -F "file=@photo.jpg" https://api.example.com/upload. Download: curl -O https://example.com/file.zip (saves with original name) or -o custom-name.zip. Resume interrupted downloads with -C -. For large files, add --progress-bar for a visual indicator.
Cookies and Sessions
Save cookies: curl -c cookies.txt -b cookies.txt for cookie jar. Send specific cookies: -b "session=abc123". This is essential for testing authentication flows that use session cookies instead of tokens.
Advanced Patterns
Follow redirects: -L. Set timeout: --connect-timeout 5 --max-time 30. Retry on failure: --retry 3 --retry-delay 2. Send data from file: -d @payload.json. Test with specific HTTP version: --http2 or --http3.
Conclusion
cURL is the Swiss Army knife of HTTP testing. Every developer should be comfortable with basic requests, header manipulation, and verbose debugging. Combined with jq, it's a powerful API testing workflow that requires no GUI tools. Bookmark these examples — you'll use them daily.