From fb665dc6e0fe80c0096b1b47292e6d1c4f7a0bd7 Mon Sep 17 00:00:00 2001 From: Roman Kozlov Date: Mon, 22 May 2023 12:43:10 +0300 Subject: [PATCH] fix response body when using go_template_json --- server/templates/go_template.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/server/templates/go_template.go b/server/templates/go_template.go index 4fa2741..56d1011 100644 --- a/server/templates/go_template.go +++ b/server/templates/go_template.go @@ -64,14 +64,15 @@ func (*goTemplateJsonEngine) Execute(request types.Request, script string) (*typ return nil, fmt.Errorf("failed to unmarshal response from dynamic template: %w", err) } - body := tmplResult["body"] - if _, ok := body.(string); !ok { - b, err := json.Marshal(body) - if err != nil { - log.WithError(err).Error("Failed to marshal response body as JSON") - return nil, fmt.Errorf("failed to marshal response body as JSON: %w", err) + if body := tmplResult["body"]; body != nil { + if _, ok := body.(string); !ok { + b, err := json.Marshal(body) + if err != nil { + log.WithError(err).Error("Failed to marshal response body as JSON") + return nil, fmt.Errorf("failed to marshal response body as JSON: %w", err) + } + tmplResult["body"] = string(b) } - tmplResult["body"] = string(b) } b, err := json.Marshal(tmplResult)