Skip to content

Commit 656cee9

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 eaaf0cd commit 656cee9

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
},
@@ -622,14 +624,16 @@ func SysHTTPHtml2Text(ctx context.Context, env []string, input string, progress
622624

623625
func SysHTTPPost(ctx context.Context, _ []string, input string, _ chan<- string) (_ string, err error) {
624626
var params struct {
625-
URL string `json:"url,omitempty"`
626-
Content string `json:"content,omitempty"`
627-
ContentType string `json:"contentType,omitempty"`
627+
URL string `json:"url,omitempty"`
628+
Content string `json:"content,omitempty"`
629+
ContentType string `json:"contentType,omitempty"`
630+
IncludeResponse string `json:"includeResponse,omitempty"`
628631
}
629632
if err := json.Unmarshal([]byte(input), &params); err != nil {
630633
return invalidArgument(input, err), nil
631634
}
632635

636+
includeResponse, _ := strconv.ParseBool(params.IncludeResponse)
633637
params.URL = fixQueries(params.URL)
634638

635639
req, err := http.NewRequestWithContext(ctx, http.MethodPost, params.URL, strings.NewReader(params.Content))
@@ -648,11 +652,30 @@ func SysHTTPPost(ctx context.Context, _ []string, input string, _ chan<- string)
648652
}
649653
defer resp.Body.Close()
650654

651-
_, _ = io.ReadAll(resp.Body)
655+
var (
656+
rawData []byte
657+
outputData string
658+
)
659+
660+
rawData, err = io.ReadAll(resp.Body)
661+
if includeResponse && err != nil {
662+
rawData = []byte(err.Error())
663+
}
664+
if includeResponse {
665+
outputData = strings.TrimSpace(string(rawData))
666+
}
667+
652668
if resp.StatusCode > 399 {
669+
if includeResponse && len(outputData) > 0 {
670+
return fmt.Sprintf("Failed to post URL %s: %s (cause: %s)", params.URL, resp.Status, outputData), nil
671+
}
653672
return fmt.Sprintf("Failed to post URL %s: %s", params.URL, resp.Status), nil
654673
}
655674

675+
if includeResponse && len(outputData) > 0 {
676+
return outputData, nil
677+
}
678+
656679
return fmt.Sprintf("Wrote %d to %s", len([]byte(params.Content)), params.URL), nil
657680
}
658681

0 commit comments

Comments
 (0)