MCPcopy Create free account
hub / github.com/devld/go-drive / vm_http

Function vm_http

script/call_http.go:17–74  ·  view source on GitHub ↗

vm_http: (ctx Context, method, url string, headers types.SM, body any) *httpResponse

(vm *VM, args Values)

Source from the content-addressed store, hash-verified

15
16// vm_http: (ctx Context, method, url string, headers types.SM, body any) *httpResponse
17func vm_http(vm *VM, args Values) any {
18 ctx := args.Get(0).Raw()
19 method := args.Get(1).String()
20 url := args.Get(2).String()
21 headers := args.Get(3).SM()
22 body := args.Get(4).Raw()
23
24 var bodyReader io.Reader
25 var contentType string
26 var errChan chan error
27
28 if body != nil {
29 if str, ok := body.(string); ok {
30 bodyReader = bytes.NewReader([]byte((str)))
31 } else if vr := GetReader(body); vr != nil {
32 bodyReader = vr
33 } else if b := GetBytes(body); b != nil {
34 bodyReader = bytes.NewReader(b)
35 } else if fd, ok := body.(*formData); ok {
36 r, w := io.Pipe()
37 errChan = make(chan error, 1)
38 bodyReader = r
39 contentType = fd.prepare(w)
40 go func() {
41 defer func() { _ = w.Close() }()
42 errChan <- fd.write()
43 }()
44 }
45 }
46 var req *http.Request
47 var e error
48
49 req, e = http.NewRequestWithContext(GetContext(ctx), method, url, bodyReader)
50 if e != nil {
51 vm.ThrowError(e)
52 }
53
54 if contentType != "" {
55 req.Header.Set("Content-Type", contentType)
56 }
57 req.Header.Set("User-Agent", gReq.DefaultUserAgent)
58 for k, v := range headers {
59 req.Header.Set(k, v)
60 }
61 resp, e := httpClient.Do(req)
62 var wErr error
63 if errChan != nil {
64 wErr = <-errChan
65 }
66 if wErr != nil {
67 vm.ThrowError(wErr)
68 }
69 if e != nil {
70 vm.ThrowError(e)
71 }
72
73 return newHttpResponse(vm, resp)
74}

Callers

nothing calls this directly

Calls 13

GetReaderFunction · 0.85
GetBytesFunction · 0.85
GetContextFunction · 0.85
newHttpResponseFunction · 0.85
RawMethod · 0.80
SMMethod · 0.80
ThrowErrorMethod · 0.80
GetMethod · 0.65
StringMethod · 0.65
prepareMethod · 0.65
CloseMethod · 0.65
writeMethod · 0.45

Tested by

no test coverage detected