MCPcopy
hub / github.com/yusing/godoxy / execHTTP

Function execHTTP

cmd/cli/cli.go:335–367  ·  view source on GitHub ↗
(ep Endpoint, reqURL string, body []byte)

Source from the content-addressed store, hash-verified

333}
334
335func execHTTP(ep Endpoint, reqURL string, body []byte) error {
336 var r io.Reader
337 if body != nil {
338 r = bytes.NewReader(body)
339 }
340 req, err := http.NewRequest(ep.Method, reqURL, r)
341 if err != nil {
342 return err
343 }
344 if body != nil {
345 req.Header.Set("Content-Type", "application/json")
346 }
347 client := &http.Client{Timeout: 30 * time.Second}
348 resp, err := client.Do(req)
349 if err != nil {
350 return err
351 }
352 defer resp.Body.Close()
353
354 payload, err := io.ReadAll(resp.Body)
355 if err != nil {
356 return err
357 }
358 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
359 if len(payload) == 0 {
360 return fmt.Errorf("%s %s failed: %s", ep.Method, ep.Path, resp.Status)
361 }
362 return fmt.Errorf("%s %s failed: %s: %s", ep.Method, ep.Path, resp.Status, strings.TrimSpace(string(payload)))
363 }
364
365 printJSON(payload)
366 return nil
367}
368
369func execWebsocket(ep Endpoint, reqURL string) error {
370 wsURL := strings.Replace(reqURL, "http://", "ws://", 1)

Callers 1

executeEndpointFunction · 0.85

Calls 4

printJSONFunction · 0.85
DoMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected