Skip to content

Commit 54e0f03

Browse files
committed
add support for form body in http requests
1 parent dbc1120 commit 54e0f03

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

checks/http.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"maps"
88
"net/http"
9+
"net/url"
910
"regexp"
1011
"strings"
1112

@@ -37,7 +38,24 @@ func runHTTPRequest(
3738
if err != nil {
3839
cobra.CheckErr("Failed to create request")
3940
}
40-
req.Header.Add("Content-Type", "application/json")
41+
req.Header.Set("Content-Type", "application/json")
42+
} else if requestStep.Request.BodyForm != nil {
43+
formValues := url.Values{}
44+
for key, val := range requestStep.Request.BodyForm {
45+
interpolatedVal := InterpolateVariables(val, variables)
46+
formValues.Add(key, interpolatedVal)
47+
}
48+
49+
encodedFormStr := formValues.Encode()
50+
var err error
51+
req, err = http.NewRequest(requestStep.Request.Method, completeURL,
52+
strings.NewReader(encodedFormStr),
53+
)
54+
if err != nil {
55+
cobra.CheckErr("Failed to create request")
56+
}
57+
58+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
4159
} else {
4260
var err error
4361
req, err = http.NewRequest(requestStep.Request.Method, completeURL, nil)

client/lessons.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ type HTTPRequest struct {
9797
FullURL string
9898
Headers map[string]string
9999
BodyJSON map[string]any
100+
BodyForm map[string]string
100101

101102
BasicAuth *HTTPBasicAuth
102103
}

0 commit comments

Comments
 (0)