Files
fn_registry/functions/infra/http_json_response.md
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

1.5 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
http_json_response function go infra 1.0.0 impure func HTTPJSONResponse(w http.ResponseWriter, status int, data any) Escribe data como JSON en el ResponseWriter con el status code dado. Setea Content-Type: application/json automaticamente.
http
json
response
server
infra
false error_go_core
encoding/json
net/http
name desc
w ResponseWriter donde se escribe la respuesta HTTP
name desc
status codigo de status HTTP (ej: 200, 201, 400, 500)
name desc
data cualquier valor serializable a JSON que se escribe como body de la respuesta
escribe la respuesta JSON directamente en w, sin valor de retorno true
escribe status code correcto
setea Content-Type application/json
serializa datos correctamente a JSON
functions/infra/http_server_test.go functions/infra/http_json_response.go

Ejemplo

func getUser(w http.ResponseWriter, r *http.Request) {
    user := map[string]string{"id": "1", "name": "Lucas"}
    HTTPJSONResponse(w, http.StatusOK, user)
}

Notas

Usa json.NewEncoder para streaming directo al ResponseWriter sin buffer intermedio. El header Content-Type debe setearse antes de WriteHeader. Errores de serializacion se propagan parcialmente al body — asegurarse de pasar tipos serializable a JSON.