curl
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
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://wp-p.info/sandbox/api.php?series=like_a_dragon&id=1"
{
"status": "ok",
"series": "like_a_dragon",
"title": "Like a Dragon",
"character": {
"id": 1,
"name": "Kiryu Kazuma",
"age": 37,
"team": "Dojima Family",
"email": "kiryu_kazuma@wp-p.info",
"bgm": "Baka Mitai"
}
}
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.
sample_post_json.sh
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer my_token_here" \
-d '{"name":"Kiryu Kazuma","age":37}' \
https://wp-p.info/sandbox/api.php
Run the following command:
bash sample_post_json.sh
{
"status": "ok",
"message": "Data received successfully.",
"method": "POST",
"received": {
"name": "Kiryu Kazuma",
"age": 37
}
}
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.
sample_fetch_user.sh
response=$(curl -s "https://wp-p.info/sandbox/api.php?series=like_a_dragon&id=1") echo "$response" | grep "name"
Run the following command:
bash sample_fetch_user.sh
"name": "Kiryu Kazuma",
Common Mistakes
Common Mistake 1: Without -o, the response is printed to the terminal instead of being saved
By default, curl writes the response body to standard output. Without -o, the file is not saved.
curl https://example.com/file.tar.gz (binary data floods the terminal)
Use -o to specify the output filename, or -O to use the remote filename.
curl -o file.tar.gz https://example.com/file.tar.gz
Common Mistake 2: curl stops without following redirects
By default, curl does not follow HTTP redirects (301/302). You receive only the redirect response.
curl https://example.com <!DOCTYPE html><html><head><meta http-equiv="refresh" ...
Add -L to follow redirects automatically.
curl -L https://example.com
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.