Learn CURL with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
cURL Counter Simulation and Theme Toggle
count=0
isDark=0
updateUI() {
echo "Counter: $count"
if [ $isDark -eq 1 ]; then
echo "Theme: Dark"
else
echo "Theme: Light"
fi
}
increment() {
count=$((count+1))
updateUI
}
decrement() {
count=$((count-1))
updateUI
}
reset() {
count=0
updateUI
}
toggleTheme() {
isDark=$((1-isDark))
updateUI
}
# Simulate actions
updateUI
increment
increment
toggleTheme
decrement
reset
# Example API call using cURL
# curl -X GET https://api.example.com/data -H "Authorization: Bearer TOKEN"
Demonstrates a simulated counter and theme toggle using cURL commands with local shell variables.
2
cURL Simple GET Request
curl -X GET https://api.example.com/data
Performs a simple GET request using cURL.
3
cURL POST Request with JSON
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d '{"name":"John","age":30}'
Sends JSON data in a POST request using cURL.
5
cURL Upload File
curl -X POST https://api.example.com/upload \
-F 'file=@/path/to/localfile.txt'
Uploads a file to a server using cURL.
6
cURL Authentication
curl -u username:password https://api.example.com/data
Performs a request with basic authentication.
7
cURL Headers Example
curl -H "Authorization: Bearer TOKEN" -H "Accept: application/json" https://api.example.com/data
Sends custom headers in a cURL request.
8
cURL Save Response to File
curl https://api.example.com/data -o response.json
Saves the response of a cURL request to a local file.
9
cURL Follow Redirects
curl -L https://short.url/example
Follows HTTP redirects automatically.
10
cURL Timeout Example
curl --max-time 10 https://api.example.com/data
Sets a timeout for the cURL request.