Skip to content

Commit ce79ae0

Browse files
Add sys.http.get and sys.http.post
1 parent 061cfc3 commit ce79ae0

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

pkg/builtin/builtin.go

+72-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package builtin
22

33
import (
44
"context"
5-
"encoding/base64"
65
"encoding/json"
76
"fmt"
7+
"io"
8+
"net/http"
89
"os"
9-
"unicode/utf8"
10+
"strings"
1011

1112
"github.com/acorn-io/gptscript/pkg/types"
1213
)
@@ -30,10 +31,7 @@ var Tools = map[string]types.Tool{
3031
return "", err
3132
}
3233

33-
if utf8.Valid(data) {
34-
return string(data), nil
35-
}
36-
return base64.StdEncoding.EncodeToString(data), nil
34+
return string(data), nil
3735
},
3836
},
3937
"sys.write": {
@@ -57,6 +55,74 @@ var Tools = map[string]types.Tool{
5755
return "", os.WriteFile(params.Filename, data, 0644)
5856
},
5957
},
58+
"sys.http.get": {
59+
Description: "Download the contents of a http or https URL",
60+
Arguments: types.ObjectSchema(
61+
"url", "The URL to download"),
62+
BuiltinFunc: func(ctx context.Context, env []string, input string) (string, error) {
63+
var params struct {
64+
URL string `json:"url,omitempty"`
65+
}
66+
if err := json.Unmarshal([]byte(input), &params); err != nil {
67+
return "", err
68+
}
69+
70+
log.Debugf("http get %s", params.URL)
71+
resp, err := http.Get(params.URL)
72+
if err != nil {
73+
return "", err
74+
}
75+
defer resp.Body.Close()
76+
if resp.StatusCode != http.StatusOK {
77+
return "", fmt.Errorf("failed to download %s: %s", params.URL, resp.Status)
78+
}
79+
80+
data, err := io.ReadAll(resp.Body)
81+
if err != nil {
82+
return "", err
83+
}
84+
85+
return string(data), nil
86+
},
87+
},
88+
"sys.http.post": {
89+
Description: "Write contents to a http or https URL using the POST method",
90+
Arguments: types.ObjectSchema(
91+
"url", "The URL to POST to",
92+
"content", "The content to POST",
93+
"contentType", "The \"content type\" of the content such as application/json or text/plain"),
94+
BuiltinFunc: func(ctx context.Context, env []string, input string) (string, error) {
95+
var params struct {
96+
URL string `json:"url,omitempty"`
97+
Content string `json:"content,omitempty"`
98+
ContentType string `json:"contentType,omitempty"`
99+
}
100+
if err := json.Unmarshal([]byte(input), &params); err != nil {
101+
return "", err
102+
}
103+
104+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, params.URL, strings.NewReader(params.Content))
105+
if err != nil {
106+
return "", err
107+
}
108+
if params.ContentType != "" {
109+
req.Header.Set("Content-Type", params.ContentType)
110+
}
111+
112+
resp, err := http.DefaultClient.Do(req)
113+
if err != nil {
114+
return "", err
115+
}
116+
defer resp.Body.Close()
117+
118+
_, _ = io.ReadAll(resp.Body)
119+
if resp.StatusCode > 399 {
120+
return "", fmt.Errorf("failed to post %s: %s", params.URL, resp.Status)
121+
}
122+
123+
return fmt.Sprintf("Wrote %d to %s", len([]byte(params.Content)), params.URL), nil
124+
},
125+
},
60126
}
61127

62128
func Builtin(name string) (types.Tool, bool) {

0 commit comments

Comments
 (0)