Clase 03 — Envío de Datos (POST, PUT, formularios, archivos)
Enviar JSON
# JSON inline
curl -s -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"nombre": "Ana", "edad": 28, "activo": true}' | jq '.json'
# JSON desde variable
DATOS='{"servicio":"api","version":"2.0","replicas":3}'
curl -s -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d "$DATOS" | jq '.json'
# JSON desde archivo
curl -s -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d @/tmp/deploy.json | jq '.json'
# JSON construido con jq
curl -s -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d "$(jq -n --arg app "mi-api" --argjson port 8080 '{app: $app, port: $port}')" | jq '.json'
Enviar Form Data
# Form URL-encoded
curl -s -X POST https://httpbin.org/post \
-d "nombre=Ana" -d "email=ana@dev.com" -d "rol=DevOps" | jq '.form'
# Con caracteres especiales (URL encode)
curl -s -X POST https://httpbin.org/post \
--data-urlencode "mensaje=Hola, ¿cómo estás?" | jq '.form'
Enviar archivos (Multipart/Form-Data)
# Subir un archivo
curl -s -X POST https://httpbin.org/post \
-F "archivo=@/tmp/test.txt" | jq '.files'
# Archivo con nombre personalizado
curl -s -X POST https://httpbin.org/post \
-F "documento=@/tmp/test.txt;filename=reporte.txt" | jq '.files'
# Múltiples archivos
curl -s -X POST https://httpbin.org/post \
-F "archivo1=@/tmp/test.txt" \
-F "archivo2=@/tmp/test.txt" | jq '.files | keys'
# Archivo + campos de formulario
curl -s -X POST https://httpbin.org/post \
-F "nombre=Ana" -F "tipo=reporte" \
-F "archivo=@/tmp/test.txt" | jq '{form: .form, files: .files}'
Enviar datos binarios
# --data-binary preserva newlines
curl -s -X POST https://httpbin.org/post --data-binary @/tmp/script.sh | jq '.data'
# Diferencia: -d elimina newlines, --data-binary los preserva
Enviar datos con stdin
# Desde un pipe
echo '{"status":"ok"}' | curl -s -X POST https://httpbin.org/post \
-H "Content-Type: application/json" -d @- | jq '.json'
# Datos generados dinámicamente
curl -s -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"hostname": "$(hostname)",
"user": "$(whoami)"
}
EOF
POST con diferentes Content-Types
# 1. application/json (APIs modernas)
curl -X POST https://httpbin.org/post -H "Content-Type: application/json" -d '{"key":"value"}'
# 2. application/x-www-form-urlencoded (formularios HTML)
curl -X POST https://httpbin.org/post -d "key=value&otro=dato"
# 3. multipart/form-data (archivos)
curl -X POST https://httpbin.org/post -F "key=value" -F "archivo=@file.txt"
# 4. text/plain
curl -X POST https://httpbin.org/post -H "Content-Type: text/plain" -d "texto plano"
# 5. application/xml
curl -X POST https://httpbin.org/post -H "Content-Type: application/xml" -d "<root><key>value</key></root>"
Ejemplo: Webhook de deploy
#!/bin/bash
WEBHOOK_URL="$SLACK_WEBHOOK"
APP="pokemon-api"
VERSION="2.1.0"
PAYLOAD=$(jq -n \
--arg app "$APP" --arg ver "$VERSION" \
--arg who "$(whoami)" --arg ts "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
'{text: "🚀 Deploy completado", attachments: [{color: "#36a64f", fields: [{title: "App", value: $app, short: true}, {title: "Versión", value: $ver, short: true}]}]}')
curl -s -X POST "$WEBHOOK_URL" -H "Content-Type: application/json" -d "$PAYLOAD"
Ejercicios
- Enviá un JSON con 5 campos diferentes a httpbin y verificá que todos llegaron
- Subí un archivo de texto a httpbin usando
-Fy verificá el contenido - Creá un script que tome variables de entorno y las envíe como JSON
- Demostrá la diferencia entre
-dy--data-binarycon un archivo multilínea