From e0ab3bb51a133c99ada1517d8bb36f68713532e0 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Fri, 14 Aug 2026 16:56:43 +0200 Subject: [PATCH] feat: added generic json payload decoder function --- internal/api/request/json.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 internal/api/request/json.go diff --git a/internal/api/request/json.go b/internal/api/request/json.go new file mode 100644 index 0000000..b9140bd --- /dev/null +++ b/internal/api/request/json.go @@ -0,0 +1,20 @@ +package request + +import ( + "encoding/json" + "fmt" + "net/http" +) + +func DecodeJSON[T any](r *http.Request) (T, error) { + var payload T + + decoder := json.NewDecoder(r.Body) + decoder.DisallowUnknownFields() + + if err := decoder.Decode(&payload); err != nil { + return payload, fmt.Errorf("error decoding payload: %w", err) + } + + return payload, nil +}