curl (Client URL) is a command-line tool used to transfer data to or from a server using various protocols (HTTP, HTTPS, FTP, etc.). For example, just some uses include:
- “GET” a URL (the rought equivalent of just pasting the URL into your browser address bar)
- Interact with an API (Usually REST, but it doesn’t have to be)
- Download a file in a scripted/automated fashion
Yes, you can find a curl tutorial just about anywhere. These are just a few of my notes for later reference, and maybe something you (dear reader) can learn from as well.
Note: while these notes are Linux specific you can also use curl via PowerShell or the Windows command line.
1. Basic Usage & Syntax
Basic GET Request
curl https://api.example.com/data
Key Concepts
- Default method:
GET - Outputs response directly to
stdout(terminal)
2. Passing a Bearer Token
This is especially useful when interacting with an API that requires this level of authentication. Simply use the -H (or --header) flag to send an Authorization header containing a Bearer token.
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/protected
Passing Multiple Headers
Of course there are other times when you need to pass more than 1 header…
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
https://api.example.com/protected
3. Saving Output / Downloading Files
Option A: Save with Remote Filename (-O / --remote-name)
Saves the file using the original filename from the URL.
curl -O https://example.com/files/document.pdf
Option B: Save with Custom Output Path (-o / --output)
Saves the output to a specific path or filename.
curl -o /path/to/local_file.pdf https://example.com/files/document.pdf
Option C: Follow Redirects (-L / --location)
Combine -L with -o or -O if the URL redirects (HTTP 301/302).
curl -L -o download.tar.gz https://example.com/download/latest
4. Capturing Output
Capture Response in a Shell Variable
response=$(curl -s https://api.example.com/data)
echo "$response"
Capture Response Body and HTTP Status Code
# Save status code to variable and body to file
http_code=$(curl -s -o response.json -w "%{http_code}" https://api.example.com/data)
echo "HTTP Status Code: $http_code"
Capture Response Headers (-i or -I)
- Include headers with body (
-i):
curl -i https://api.example.com/data
- Fetch headers only (
-Ior--head):
curl -I https://api.example.com/data
5. Quick Reference Flags
| Flag | Long Name | Description |
|---|---|---|
-X | --request | Specifies HTTP method (GET, POST, PUT, DELETE) |
-H | --header | Adds custom HTTP headers |
-d | --data | Sends POST data (JSON, form fields) |
-o | --output | Writes output to specified file path |
-O | --remote-name | Writes output to file named as in remote URL |
-s | --silent | Mutes progress meter and error messages |
-v | --verbose | Detailed log of request and response details |
-L | --location | Follows HTTP redirects |