Linux curl Command Examples

curl is the go-to command-line tool for transferring data with URLs. It supports HTTP, HTTPS, FTP, and dozens of other protocols. Developers use it daily for API testing, file downloads, form submissions, and debugging network requests — all without leaving the terminal.

LinuxcurlHTTPAPI
Basic Requests
Simple GET request
curl https://api.example.com/data
The most basic curl usage: sends an HTTP GET request and prints the response body to stdout. The fastest way to test if an API endpoint is reachable and see its raw response without any browser or GUI tool.
Follow redirects
curl -L https://example.com
By default, curl does not follow HTTP 3xx redirects. The -L flag instructs curl to follow all redirects until it reaches the final destination. Essential when working with URLs that redirect to CDNs or HTTPS versions.
Show only response headers
curl -I https://example.com
The -I flag sends a HEAD request, printing only the response headers. Useful for checking HTTP status codes, Content-Type, cache headers, and server information without downloading the full response body.
Verbose output for debugging
curl -v https://example.com
The -v flag shows the full request/response exchange including headers and TLS details. Lines with > are sent data; lines with < are received. Invaluable for debugging API authentication and redirect issues.
POST & Authentication
POST JSON data
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice","role":"admin"}' \
  https://api.example.com/users
Use -X POST to set the HTTP method, -H to add headers, and -d to send a request body. Setting Content-Type: application/json tells the server to expect JSON. This is the standard pattern for REST API calls.
POST form data
curl -X POST -d "username=alice&password=secret" https://example.com/login
Sends URL-encoded form data, matching what HTML form submissions send by default. The -d flag automatically sets the method to POST. Use this to simulate form submissions when testing login endpoints or webhooks.
Bearer token authentication
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/profile
Add an Authorization header with a Bearer token to authenticate API requests. Replace YOUR_TOKEN with your actual JWT or API token. This is the standard authentication method for most modern REST APIs.
File Operations
Download a file (keep original name)
curl -O https://example.com/file.zip
The -O flag saves the file using its original filename from the URL path. The file is saved in the current directory. Add -L if the download URL involves redirects before serving the file content.
Save with a custom filename
curl -o myfile.zip https://example.com/download?id=123
Lowercase -o filename saves to a custom path you specify. Unlike -O, this works even when the URL has no clean filename — such as query-string-based download endpoints.
Set a request timeout
curl --max-time 10 https://api.example.com/slow-endpoint
The --max-time flag aborts after the specified number of seconds. Essential in scripts where a hanging request would block execution. Use --connect-timeout to limit only the initial TCP connection phase.

How to Use

  1. curl is pre-installed on Linux and macOS. On Windows, use WSL or download from curl.se.
  2. Start with curl URL to see the raw response from any endpoint.
  3. Add -v to see full request/response details when troubleshooting.
  4. Use -H for headers, -d for request body, and -X to override the HTTP method.
  5. Pipe output to jq for pretty JSON: curl URL | jq .

Frequently Asked Questions

What is the difference between curl -O and curl -o?

Capital -O saves the file using the filename from the URL path. Lowercase -o filename saves to a custom filename you specify. Use -o when the URL doesn't have a clean filename in its path, or when saving to a specific directory.

How do I send a JSON request body from a file?

Use -d @filename.json — the @ prefix tells curl to read body data from a file rather than treating the string as literal data. Always pair this with -H "Content-Type: application/json" so the server knows how to parse it.

How do I handle self-signed SSL certificates in development?

Use -k or --insecure to skip certificate verification. This is acceptable in local development environments but never use it in production scripts as it disables all TLS security checks. For production, provide the CA cert with --cacert ca.pem.

Related Tools