curl
curl is a tool for sending and receiving HTTP/HTTPS requests from the command line. You can use it for a wide range of tasks, including calling Web APIs, downloading files, and submitting form data.
Syntax
Send a basic GET request.
curl URL
Save the response to a file.
curl -o output_file URL
Save using the filename from the URL.
curl -O URL
Follow redirects.
curl -L URL
Send form data via a POST request.
curl -X POST -d "key=value&key2=value2" URL
Send JSON via a POST request.
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL
Add a header.
curl -H "Authorization: Bearer token" URL
Options
| Option | Description |
|---|---|
| -o file | Saves the response body to the specified file. |
| -O | Saves the file to the current directory using the filename from the URL. |
| -L | Follows redirects (301/302). |
| -s | Runs in silent mode, suppressing progress and error output. |
| -S | Used with -s to show errors only. |
| -w "%{http_code}" | Appends formatted output such as the HTTP status code. |
| -X method | Specifies the HTTP method (GET, POST, PUT, DELETE, etc.). |
| -H "header" | Adds a request header. |
| -d "data" | Specifies the body data for a POST request. |
| -F "field=value" | Sends data as multipart/form-data. |
| -u user:password | Specifies credentials for Basic authentication. |
| -I | Retrieves response headers only (HEAD request). |
| -v | Shows detailed request/response information (for debugging). |
| --max-time seconds | Sets the timeout duration. |
| -k | Skips SSL certificate verification (for development environments). |
Sample Code
Sends a simple GET request. The response body is printed to standard output.
curl https://api.example.com/users/1
{"id":1,"name":"Alice","email":"alice@example.com"}
Checks only the HTTP status code. The -s option enables silent mode, and -o /dev/null discards the response body.
curl -s -o /dev/null -w "%{http_code}" https://example.com
200
Inspects the response headers (uses -I to send a HEAD request).
curl -I https://example.com HTTP/2 200 content-type: text/html; charset=UTF-8 content-length: 12345 last-modified: Thu, 01 Jan 2026 00:00:00 GMT
Sends JSON data via POST with Bearer token authentication.
post_json.sh
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer my_token_here" \
-d '{"name":"Alice","age":30}' \
https://api.example.com/users
bash post_json.sh
{"id":42,"name":"Alice","age":30,"created_at":"2026-03-06"}
Downloads a file. -L follows redirects, and -sS shows errors only.
curl -sSL -o /tmp/file.tar.gz https://example.com/release.tar.gz
Uploads a file (multipart/form-data).
curl -F "file=@report.pdf" -F "title=Report" https://example.com/upload
Accesses a resource using Basic authentication.
curl -u admin:password https://example.com/admin/api
Stores the response in a variable for further processing.
fetch_user.sh
response=$(curl -s https://api.example.com/users/1)
echo "$response" | grep "name"
bash fetch_user.sh
"name": "Alice",
Notes
When using curl in scripts, combining -s (silent) and -S (show errors) as -sS is a common pattern. It is also common to use -w "%{http_code}" to check the status code and handle any response other than 200 as an error.
Only use -k (skip SSL verification) in development environments — never in production. For downloading files, wget is also a handy option.
If you find any errors or copyright issues, please contact us.