How to Use curl for HTTP Requests
In this article
This guide shows you how to use the curl command to make HTTP requests from your terminal. You'll learn the core syntax, common options, and practical examples for GET, POST, and other methods. No prior experience with curl is needed, just a terminal and an internet connection.
What You'll Need
- A terminal or command prompt (Linux, macOS, or Windows with WSL or Git Bash).
curlinstalled (it's pre-installed on most systems; check withcurl --version).- Optional: a local or public API endpoint to test against (e.g.,
https://api.example.com).
Step-by-Step
- Open your terminal. On Linux/macOS, use Terminal. On Windows, use Command Prompt, PowerShell, or better, WSL or Git Bash.
- Test curl is installed. Run
curl --version. If you see version info, you're good. If not, install curl via your package manager (e.g.,sudo apt install curlon Ubuntu). - Make a simple GET request. Type
curl https://api.github.comand press Enter. You'll see the raw response body (usually JSON or HTML) printed to the terminal. - Include HTTP headers. Use
-ito include response headers, or-Ito fetch only headers. Example:curl -I https://example.comshows the status line and headers. - Save the output to a file. Use
-o filenameto save the body, or-Oto save using the remote filename. Example:curl -o page.html https://example.com. - Send a POST request. Use
-X POSTto specify the method, and-dto send data. Example:curl -X POST -d "name=John&age=30" https://httpbin.org/post. - Send JSON data. Use
-H "Content-Type: application/json"and the-dflag with JSON string. Example:curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://httpbin.org/post. - Add custom headers. Use
-Hfor each header. Example:curl -H "Authorization: Bearer TOKEN" https://api.example.com/data. - Follow redirects. Use
-Lto follow HTTP 3xx redirects automatically. Example:curl -L https://example.com/redirect. - Set a timeout. Use
--max-time 10to limit the request to 10 seconds. Example:curl --max-time 5 https://example.com. - Verbose output for debugging. Use
-vto see all request and response details, including TLS handshake and headers. Example:curl -v https://example.com. - Use a proxy. If you're behind a proxy, set it with
-x. Example:curl -x http://proxy.example.com:8080 https://example.com. - Upload a file. Use
-Fto send a file as multipart/form-data. Example:curl -F "file=@/path/to/file.txt" https://httpbin.org/post. - Test with a public API. Try
curl https://api.github.com/users/octocatto see a JSON response. This is a good way to verify your curl works.
Common Mistakes
- Forgetting quotes around URLs with special characters. For example,
curl https://example.com/?a=1&b=2may break because the shell interprets&. Always quote the URL:curl "https://example.com/?a=1&b=2". - Using single quotes inside single quotes. When sending JSON, you need to escape double quotes or use a file. Example:
curl -d '{"key":"value"}'works, butcurl -d '{'key':'value'}'fails. - Forgetting to set Content-Type for JSON. If you send JSON without the header, the server may not parse it correctly.
- Not following redirects. If you see a 3xx response and no content, add
-L. - Using
-X POSTwith-I.-Iforces a HEAD request, so don't combine them unless you intend that.
Tips
- Use
curl -sto suppress progress output for cleaner scripts. - Combine options:
curl -s -o /dev/null -w "%{http_code}" https://example.comprints just the HTTP status code. - Use
--data-urlencodefor query parameters that need encoding. Example:curl -G --data-urlencode "query=hello world" https://api.example.com/search. - For repeated testing, create a shell script with your curl commands.
- Check the exit code:
echo $?after curl. 0 means success, other numbers indicate errors.
Troubleshooting
- If you get "curl: command not found", install curl. On Ubuntu:
sudo apt install curl. On macOS, it's pre-installed. On Windows, use WSL or Git Bash. - If the request hangs, try adding
--max-time 10to force a timeout. Check your internet connection and firewall. - If you get a 403 Forbidden, the server may require a User-Agent or authentication. Try
-A "Mozilla/5.0"or add an API key. - If you get a 404 Not Found, check the URL path. Ensure you're using the correct endpoint.
- If you get SSL errors, try
-kto ignore certificate verification (not recommended for production). For self-signed certs, use--cacertto specify a CA bundle. - If the output is binary or garbled, the server may be sending compressed data. Use
--compressedto let curl handle gzip/deflate.
Frequently Asked Questions
How do I see the response headers only?
Use curl -I https://example.com to fetch only the headers.
How do I send a PUT or DELETE request?
Use -X PUT or -X DELETE. For example: curl -X DELETE https://api.example.com/resource/1.
How do I download a file with curl?
Use curl -O https://example.com/file.zip to save with the remote filename, or -o custom.zip to specify a name.
How do I use curl with authentication?
For basic auth, use -u username:password. For token auth, add a header: -H "Authorization: Bearer TOKEN".
How do I send a cookie?
Use -b "name=value" to send a cookie, or -c cookies.txt to save cookies from the response.
How do I make a request to a local server?
Use curl http://localhost:8080 or http://127.0.0.1:8080. Make sure the server is running.