Skip to content

Commit 51444a4

Browse files
committed
fixed query templating overwriting body
1 parent 398c164 commit 51444a4

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

internals/proxy/middlewares/template.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ func (data TemplateMiddleware) Use() http.Handler {
4242
bodyData, modified = templateJSON(body.Data, VARIABLES)
4343

4444
if modified {
45-
modifiedBody = true
45+
modifiedBody = true
4646
}
4747
}
4848

4949
if req.URL.RawQuery != "" {
5050
var modified bool
5151

52-
req.URL.RawQuery, bodyData, modified = templateQuery(req.URL, VARIABLES)
52+
req.URL.RawQuery, bodyData, modified = templateQuery(bodyData, req.URL, VARIABLES)
5353

5454
if modified {
55-
modifiedBody = true
55+
modifiedBody = true
5656
}
5757
}
5858

@@ -171,11 +171,9 @@ func templatePath(reqUrl *url.URL, VARIABLES interface{}) (string, bool) {
171171
return reqPath, modified
172172
}
173173

174-
func templateQuery(reqUrl *url.URL, VARIABLES interface{}) (string, map[string]interface{}, bool) {
174+
func templateQuery(data map[string]interface{}, reqUrl *url.URL, VARIABLES interface{}) (string, map[string]interface{}, bool) {
175175
var modified bool
176176

177-
data := map[string]interface{}{}
178-
179177
decodedQuery, _ := url.QueryUnescape(reqUrl.RawQuery)
180178

181179
log.Debug("Decoded Query: ", decodedQuery)

utils/utils.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@ package utils
77

88
import (
99
"encoding/json"
10-
"regexp"
1110
"strings"
1211
)
1312

1413
func StringToArray(sliceStr string) []string {
15-
if sliceStr == "" {
16-
return nil
17-
}
18-
19-
re, err := regexp.Compile(`\s+`)
20-
21-
if err != nil {
22-
return nil
23-
}
14+
if sliceStr == "" {
15+
return nil
16+
}
2417

25-
normalized := re.ReplaceAllString(sliceStr, "")
18+
rawItems := strings.Split(sliceStr, ",")
19+
items := make([]string, 0, len(rawItems))
2620

27-
tokens := strings.Split(normalized, ",")
21+
for _, item := range rawItems {
22+
trimmed := strings.TrimSpace(item)
23+
if trimmed != "" {
24+
items = append(items, trimmed)
25+
}
26+
}
2827

29-
return tokens
28+
return items
3029
}
3130

3231
func GetJsonSafe[T any](jsonStr string) (T, error) {

0 commit comments

Comments
 (0)