Files
fn_registry/functions/infra/http_parse_body.go
T
egutierrez b074313c01 feat: funciones impuras HTTP — response, parse, logger, router, serve
Seis funciones de servidor HTTP con tests usando httptest:
- HTTPJSONResponse: escribe JSON con Content-Type y status code
- HTTPErrorResponse: escribe HTTPError como JSON estructurado
- HTTPParseBody: decode JSON con limite de bytes y campos estrictos
- HTTPLoggerMiddleware: loguea method/path/status/duration a io.Writer
- HTTPRouter: crea ServeMux con rutas Go 1.22+ (METHOD /path)
- HTTPServe: ListenAndServe con graceful shutdown por contexto

23 tests pasando, solo stdlib net/http.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 01:57:47 +02:00

27 lines
777 B
Go

package infra
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// HTTPParseBody decodifica el body JSON de r en dst, limitando la lectura a maxBytes.
// Retorna error si el body excede maxBytes, si el JSON es invalido, o si hay error de lectura.
func HTTPParseBody(r *http.Request, dst any, maxBytes int64) error {
r.Body = http.MaxBytesReader(nil, r.Body, maxBytes)
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
if err := dec.Decode(dst); err != nil {
return fmt.Errorf("parse body: %w", err)
}
// Verificar que no queda mas data despues del primer objeto JSON
if dec.More() {
return fmt.Errorf("parse body: request body must contain a single JSON object")
}
// Consumir el resto para liberar el body
io.Copy(io.Discard, r.Body)
return nil
}