From 21e0cdc733bb551a025cf8a85674c2b8ce468c39 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 7 Aug 2025 18:11:59 +0200 Subject: [PATCH] feat: allow to handle request using json body --- form/form.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/form/form.go b/form/form.go index 348869f..168c7f2 100644 --- a/form/form.go +++ b/form/form.go @@ -16,6 +16,8 @@ package form // along with this program. If not, see . import ( + "encoding/json" + "io/ioutil" "maps" "net/http" "net/url" @@ -32,6 +34,7 @@ type Form struct { GlobalFields []*Field Errors []validation.Error Method string + JsonRequest bool Action string Name string Options []*Option @@ -201,6 +204,12 @@ func (f *Form) Mount(data any) error { return nil } +func (f *Form) WithJsonRequest() *Form { + f.JsonRequest = true + + return f +} + // Copies datas from the form to a struct func (f *Form) Bind(data any) error { toBind := make(map[string]any) @@ -216,11 +225,30 @@ func (f *Form) Bind(data any) error { func (f *Form) HandleRequest(req *http.Request) { var data url.Values - if f.Method != "GET" { - req.ParseForm() - data = req.Form + if f.JsonRequest { + body, err := ioutil.ReadAll(req.Body) + + if err != nil { + return + } + + mapping := make(map[string]any) + err = json.Unmarshal(body, &mapping) + + if err != nil { + return + } + + data = url.Values{} + util.MapToUrlValues(&data, f.Name, mapping) } else { - data = req.URL.Query() + switch f.Method { + case "GET": + data = req.URL.Query() + default: + req.ParseForm() + data = req.Form + } } isSubmitted := false