Add support body to expr fetch func

This commit is contained in:
Alex X
2025-04-22 16:37:10 +03:00
parent 70b4bf779e
commit 545a105ba0

View File

@@ -6,17 +6,23 @@ import (
"io" "io"
"net/http" "net/http"
"regexp" "regexp"
"strings"
"github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/AlexxIT/go2rtc/pkg/tcp"
"github.com/expr-lang/expr" "github.com/expr-lang/expr"
) )
func newRequest(method, url string, headers map[string]any) (*http.Request, error) { func newRequest(method, url string, headers map[string]any, body string) (*http.Request, error) {
var rd io.Reader
if method == "" { if method == "" {
method = "GET" method = "GET"
} }
if body != "" {
rd = strings.NewReader(body)
}
req, err := http.NewRequest(method, url, nil) req, err := http.NewRequest(method, url, rd)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -55,7 +61,8 @@ var Options = []expr.Option{
options := params[1].(map[string]any) options := params[1].(map[string]any)
method, _ := options["method"].(string) method, _ := options["method"].(string)
headers, _ := options["headers"].(map[string]any) headers, _ := options["headers"].(map[string]any)
req, err = newRequest(method, url, headers) body, _ := options["body"].(string)
req, err = newRequest(method, url, headers, body)
} else { } else {
req, err = http.NewRequest("GET", url, nil) req, err = http.NewRequest("GET", url, nil)
} }