Skip to content

Commit 52f04d9

Browse files
committed
feat(sys.http.post): add option to include response in output
Added a new parameter `includeResponse` to the SysHTTPPost function to allow inclusion of the HTTP POST response in the output. This provides more flexibility in handling responses directly within the tool.
1 parent 61592f1 commit 52f04d9

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

pkg/builtin/builtin.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path/filepath"
1616
"runtime"
1717
"sort"
18+
"strconv"
1819
"strings"
1920
"time"
2021

@@ -137,7 +138,8 @@ var tools = map[string]types.Tool{
137138
Arguments: types.ObjectSchema(
138139
"url", "The URL to POST to",
139140
"content", "The content to POST",
140-
"contentType", "The \"content type\" of the content such as application/json or text/plain"),
141+
"contentType", "The \"content type\" of the content such as application/json or text/plain",
142+
"includeResponse", "true/false, include POST response to output. Default false"),
141143
},
142144
BuiltinFunc: SysHTTPPost,
143145
},
@@ -616,14 +618,16 @@ func SysHTTPHtml2Text(ctx context.Context, env []string, input string, progress
616618

617619
func SysHTTPPost(ctx context.Context, _ []string, input string, _ chan<- string) (_ string, err error) {
618620
var params struct {
619-
URL string `json:"url,omitempty"`
620-
Content string `json:"content,omitempty"`
621-
ContentType string `json:"contentType,omitempty"`
621+
URL string `json:"url,omitempty"`
622+
Content string `json:"content,omitempty"`
623+
ContentType string `json:"contentType,omitempty"`
624+
IncludeResponse string `json:"includeResponse,omitempty"`
622625
}
623626
if err := json.Unmarshal([]byte(input), &params); err != nil {
624627
return invalidArgument(input, err), nil
625628
}
626629

630+
includeResponse, _ := strconv.ParseBool(params.IncludeResponse)
627631
params.URL = fixQueries(params.URL)
628632

629633
req, err := http.NewRequestWithContext(ctx, http.MethodPost, params.URL, strings.NewReader(params.Content))
@@ -642,11 +646,30 @@ func SysHTTPPost(ctx context.Context, _ []string, input string, _ chan<- string)
642646
}
643647
defer resp.Body.Close()
644648

645-
_, _ = io.ReadAll(resp.Body)
649+
var (
650+
rawData []byte
651+
outputData string
652+
)
653+
654+
rawData, err = io.ReadAll(resp.Body)
655+
if includeResponse && err != nil {
656+
rawData = []byte(err.Error())
657+
}
658+
if includeResponse {
659+
outputData = strings.TrimSpace(string(rawData))
660+
}
661+
646662
if resp.StatusCode > 399 {
663+
if includeResponse && len(outputData) > 0 {
664+
return fmt.Sprintf("Failed to post URL %s: %s (cause: %s)", params.URL, resp.Status, outputData), nil
665+
}
647666
return fmt.Sprintf("Failed to post URL %s: %s", params.URL, resp.Status), nil
648667
}
649668

669+
if includeResponse && len(outputData) > 0 {
670+
return outputData, nil
671+
}
672+
650673
return fmt.Sprintf("Wrote %d to %s", len([]byte(params.Content)), params.URL), nil
651674
}
652675

0 commit comments

Comments
 (0)