# Test REST API endpoints
curl -X GET https://api.example.com/users
curl -X POST -d '\\\\{"name":"John"\\\\}' https://api.example.com/users
curl -X PUT -d '\\\\{"name":"Jane"\\\\}' https://api.example.com/users/1
curl -X DELETE https://api.example.com/users/1
# Test with different content types
curl -H "Accept: application/json" https://api.example.com/users
curl -H "Accept: application/xml" https://api.example.com/users
Essais de performance
# Measure response time
curl -w "Total time: %\\\\{time_total\\\\}s\n" -o /dev/null -s https://example.com
# Test multiple requests
for i in \\\\{1..10\\\\}; do
curl -w "%\\\\{time_total\\\\}\n" -o /dev/null -s https://example.com
done
Gestion des erreurs
# Check HTTP status codes
http_code=$(curl -s -o /dev/null -w "%\\\\{http_code\\\\}" https://example.com)
if [ $http_code -eq 200 ]; then
echo "Success"
else
echo "Error: HTTP $http_code"
fi
#!/bin/bash
# API testing script
BASE_URL="https://api.example.com"
API_KEY="your-api-key"
# Function to make authenticated requests
api_request() \\\\{
local method=$1
local endpoint=$2
local data=$3
curl -X "$method" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
$\\\\{data:+-d "$data"\\\\} \
"$BASE_URL$endpoint"
\\\\}
# Usage examples
api_request GET "/users"
api_request POST "/users" '\\\\{"name":"John","email":"john@example.com"\\\\}'
Pratiques exemplaires en matière de sécurité
Authentification sécurisée
# Use environment variables for sensitive data
export API_TOKEN="your-secret-token"
curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com
# Read credentials from file
curl -K credentials.txt https://api.example.com
# credentials.txt:
# header = "Authorization: Bearer your-token"
Vérification du certificat
# Always verify SSL certificates in production
curl --cacert /path/to/ca-bundle.crt https://api.example.com
# For development only (not recommended for production)
curl -k https://self-signed.example.com
Cas d'utilisation courante
Scraping Web
# Download webpage
curl -L https://example.com > page.html
# Follow redirects and save cookies
curl -L -c cookies.txt -b cookies.txt https://example.com
# Set user agent to avoid blocking
curl -H "User-Agent: Mozilla/5.0 (compatible; bot)" https://example.com
Intégration de l'API
# GitHub API example
curl -H "Authorization: token your-github-token" \
https://api.github.com/user/repos
# Weather API example
curl "https://api.openweathermap.org/data/2.5/weather?q=London&appid=your-api-key"
# Slack webhook example
curl -X POST \
-H "Content-Type: application/json" \
-d '\\\\{"text":"Hello from curl!"\\\\}' \
https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK
Transfert de fichiers
# Upload to cloud storage
curl -X PUT \
-H "Authorization: Bearer token" \
--upload-file document.pdf \
https://api.storage.com/files/document.pdf
# Download with progress bar
curl --progress-bar -O https://example.com/largefile.zip
Dépannage
Questions communes
Problem
Solution
SSL certificate errors
Use -k for testing, fix certificates for production
Connection timeout
Increase --connect-timeout value
Slow downloads
Use --limit-rate to control bandwidth
Authentication failures
Check credentials and authentication method
Débogage des commandes
# Verbose output for debugging
curl -v https://example.com
# Trace all network activity
curl --trace trace.txt https://example.com
# Show only headers
curl -I https://example.com
# Test connectivity
curl -I --connect-timeout 5 https://example.com