MCPcopy Create free account
hub / github.com/LMCache/LMCache / HTTPGetJSON

Function HTTPGetJSON

operator/test/utils/http.go:52–76  ·  view source on GitHub ↗

HTTPGetJSON issues a GET against url and decodes the response body into out. Returns an error if the request fails, the status is not 2xx, or the body is not valid JSON. The caller owns out's lifetime.

(ctx context.Context, url string, out any)

Source from the content-addressed store, hash-verified

50// into out. Returns an error if the request fails, the status is not
51// 2xx, or the body is not valid JSON. The caller owns out's lifetime.
52func HTTPGetJSON(ctx context.Context, url string, out any) error {
53 req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
54 if err != nil {
55 return fmt.Errorf("build GET %s: %w", url, err)
56 }
57 resp, err := httpJSONClient.Do(req)
58 if err != nil {
59 return fmt.Errorf("GET %s: %w", url, err)
60 }
61 defer func() { _ = resp.Body.Close() }()
62 body, err := io.ReadAll(resp.Body)
63 if err != nil {
64 return fmt.Errorf("read GET %s body: %w", url, err)
65 }
66 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
67 return fmt.Errorf("GET %s: status %d body=%s", url, resp.StatusCode, string(body))
68 }
69 if out == nil {
70 return nil
71 }
72 if err := json.Unmarshal(body, out); err != nil {
73 return fmt.Errorf("decode JSON from GET %s: %w\nbody=%s", url, err, string(body))
74 }
75 return nil
76}
77
78// HTTPGetText issues a GET against url and returns the raw response
79// body as a string. Used for endpoints that don't speak JSON

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected