Clase 02 — Headers y Autenticación
Los headers son metadatos que viajan con cada petición y respuesta HTTP. Contienen información sobre el contenido, autenticación, cache, cookies, y más.
Ver headers
# Solo headers de respuesta
curl -I https://httpbin.org/get
# Headers + body
curl -i https://httpbin.org/get
# Verbose: headers enviados Y recibidos
curl -v https://httpbin.org/get 2>&1 | grep -E "^[<>]"
# Guardar headers en archivo
curl -s -D /tmp/headers.txt https://httpbin.org/get > /dev/null
Enviar headers personalizados (-H)
curl -s \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "X-Request-ID: req-$(date +%s)" \
-H "X-Custom-Header: mi-valor" \
https://httpbin.org/headers | jq '.headers'
# Sobreescribir User-Agent
curl -s -H "User-Agent: MiApp/1.0" https://httpbin.org/user-agent | jq '.'
# Eliminar un header default
curl -s -H "Accept:" https://httpbin.org/headers | jq '.headers'
Autenticación
1. Basic Auth
curl -s -u "usuario:contraseña" https://httpbin.org/basic-auth/usuario/contraseña | jq '.'
# Equivalente manual
curl -s -H "Authorization: Basic $(echo -n 'usuario:contraseña' | base64)" \
https://httpbin.org/basic-auth/usuario/contraseña | jq '.'
2. Bearer Token (JWT, OAuth2)
curl -s -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..." \
https://httpbin.org/bearer | jq '.'
# GitHub con token
export GITHUB_TOKEN="ghp_xxxxxxxxxx"
curl -s -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/user | jq '{login, name}'
3. API Key
# En header
curl -s -H "X-API-Key: mi-api-key-123" \
https://httpbin.org/headers | jq '.headers["X-Api-Key"]'
# En query parameter
curl -s "https://api.ejemplo.com/datos?api_key=mi-api-key-123"
4. Digest Auth
curl -s --digest -u "usuario:contraseña" \
https://httpbin.org/digest-auth/auth/usuario/contraseña | jq '.'
5. Certificados de cliente (mTLS)
curl -s \
--cert /path/to/client.crt \
--key /path/to/client.key \
--cacert /path/to/ca.crt \
https://api.segura.com/datos
Cookies
# Enviar una cookie
curl -s -b "session=abc123" https://httpbin.org/cookies | jq '.'
# Guardar cookies del servidor
curl -s -c /tmp/cookies.txt https://httpbin.org/cookies/set?token=xyz123
# Usar cookies guardadas
curl -s -b /tmp/cookies.txt https://httpbin.org/cookies | jq '.'
Ejemplo: Verificar headers de seguridad
#!/bin/bash
URL="${1:-https://github.com}"
echo "Verificando headers de seguridad: $URL"
headers=$(curl -sI "$URL")
check_header() {
local nombre="$1"
if echo "$headers" | grep -qi "$nombre"; then
valor=$(echo "$headers" | grep -i "$nombre" | head -1 | cut -d: -f2- | xargs)
printf " ✅ %-40s → %s\n" "$nombre" "$valor"
else
printf " ❌ %-40s → FALTANTE\n" "$nombre"
fi
}
check_header "Strict-Transport-Security"
check_header "Content-Security-Policy"
check_header "X-Content-Type-Options"
check_header "X-Frame-Options"
check_header "X-XSS-Protection"
check_header "Referrer-Policy"
Ejercicios
- Usá
curl -vpara ver todos los headers que se intercambian conhttps://google.com - Hacé una petición a httpbin con Basic Auth
- Enviá 3 headers personalizados a httpbin y verificá que llegaron
- Creá un script que verifique los headers de seguridad de 3 sitios web