Add basic auth support to custom API

pull/624/merge
Svilen Markov 2025-08-06 04:43:35 +07:00
parent f0d0b9f1ba
commit 3331670823
1 changed files with 15 additions and 2 deletions

@ -36,8 +36,12 @@ type CustomAPIRequest struct {
Body any `yaml:"body"`
MockResponse string `yaml:"mock-response"`
SkipJSONValidation bool `yaml:"skip-json-validation"`
bodyReader io.ReadSeeker `yaml:"-"`
httpRequest *http.Request `yaml:"-"`
BasicAuth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"basic-auth"`
bodyReader io.ReadSeeker `yaml:"-"`
httpRequest *http.Request `yaml:"-"`
}
type customAPIWidget struct {
@ -190,6 +194,10 @@ func (req *CustomAPIRequest) initialize() error {
httpReq.Header.Add(key, value)
}
if req.BasicAuth.Username != "" || req.BasicAuth.Password != "" {
httpReq.SetBasicAuth(req.BasicAuth.Username, req.BasicAuth.Password)
}
req.httpRequest = httpReq
return nil
@ -708,6 +716,11 @@ var customAPITemplateFuncs = func() template.FuncMap {
return req
},
"withBasicAuth": func(username, password string, req *CustomAPIRequest) *CustomAPIRequest {
req.BasicAuth.Username = username
req.BasicAuth.Password = password
return req
},
"getResponse": func(req *CustomAPIRequest) *customAPIResponseData {
err := req.initialize()
if err != nil {