TechBuzzPro

How to Use curl for HTTP Requests

· 4 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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).
  • curl installed (it's pre-installed on most systems; check with curl --version).
  • Optional: a local or public API endpoint to test against (e.g., https://api.example.com).

Step-by-Step

  1. Open your terminal. On Linux/macOS, use Terminal. On Windows, use Command Prompt, PowerShell, or better, WSL or Git Bash.
  2. 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 curl on Ubuntu).
  3. Make a simple GET request. Type curl https://api.github.com and press Enter. You'll see the raw response body (usually JSON or HTML) printed to the terminal.
  4. Include HTTP headers. Use -i to include response headers, or -I to fetch only headers. Example: curl -I https://example.com shows the status line and headers.
  5. Save the output to a file. Use -o filename to save the body, or -O to save using the remote filename. Example: curl -o page.html https://example.com.
  6. Send a POST request. Use -X POST to specify the method, and -d to send data. Example: curl -X POST -d "name=John&age=30" https://httpbin.org/post.
  7. Send JSON data. Use -H "Content-Type: application/json" and the -d flag with JSON string. Example: curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://httpbin.org/post.
  8. Add custom headers. Use -H for each header. Example: curl -H "Authorization: Bearer TOKEN" https://api.example.com/data.
  9. Follow redirects. Use -L to follow HTTP 3xx redirects automatically. Example: curl -L https://example.com/redirect.
  10. Set a timeout. Use --max-time 10 to limit the request to 10 seconds. Example: curl --max-time 5 https://example.com.
  11. Verbose output for debugging. Use -v to see all request and response details, including TLS handshake and headers. Example: curl -v https://example.com.
  12. Use a proxy. If you're behind a proxy, set it with -x. Example: curl -x http://proxy.example.com:8080 https://example.com.
  13. Upload a file. Use -F to send a file as multipart/form-data. Example: curl -F "file=@/path/to/file.txt" https://httpbin.org/post.
  14. Test with a public API. Try curl https://api.github.com/users/octocat to 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=2 may 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, but curl -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 POST with -I. -I forces a HEAD request, so don't combine them unless you intend that.

Tips

  • Use curl -s to suppress progress output for cleaner scripts.
  • Combine options: curl -s -o /dev/null -w "%{http_code}" https://example.com prints just the HTTP status code.
  • Use --data-urlencode for 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 10 to 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 -k to ignore certificate verification (not recommended for production). For self-signed certs, use --cacert to specify a CA bundle.
  • If the output is binary or garbled, the server may be sending compressed data. Use --compressed to 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.