diff --git a/internals/proxy/middlewares/mapping.go b/internals/proxy/middlewares/mapping.go index 5f47666..53f7a01 100644 --- a/internals/proxy/middlewares/mapping.go +++ b/internals/proxy/middlewares/mapping.go @@ -61,9 +61,14 @@ func mappingHandler(next http.Handler) http.Handler { if modifiedBody { body.Data = bodyData - log.Debug("Applied Data Aliasing: ", body.Data) + err := body.Write(req) + + if err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } - body.Write(req) + log.Debug("Applied Data Aliasing: ", body.Data) } next.ServeHTTP(w, req) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index eb62451..481c216 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -60,9 +60,14 @@ func messageHandler(next http.Handler) http.Handler { if modifiedBody { body.Data = bodyData - log.Debug("Applied Message Templating: ", body.Data) + err := body.Write(req) + + if err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } - body.Write(req) + log.Debug("Applied Message Templating: ", body.Data) } next.ServeHTTP(w, req) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 633ddb4..9ab980d 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -72,9 +72,14 @@ func templateHandler(next http.Handler) http.Handler { if modifiedBody { body.Data = bodyData - log.Debug("Applied Body Templating: ", body.Data) + err := body.Write(req) - body.Write(req) + if err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } + + log.Debug("Applied Body Templating: ", body.Data) } if req.URL.Path != "" { diff --git a/utils/request/request.go b/utils/request/request.go index d068e79..8dd8514 100644 --- a/utils/request/request.go +++ b/utils/request/request.go @@ -30,7 +30,15 @@ func (body Body) ToString() string { return string(body.Raw) } -func (body Body) Write(req *http.Request) { +func (body *Body) Write(req *http.Request) error { + newBody, err := CreateBody(body.Data) + + if err != nil { + return err + } + + body = &newBody + bodyLength := len(body.Raw) if req.ContentLength != int64(bodyLength) { @@ -39,6 +47,8 @@ func (body Body) Write(req *http.Request) { } req.Body = io.NopCloser(bytes.NewReader(body.Raw)) + + return nil } func CreateBody(data map[string]any) (Body, error) {