engine_client.go
TEXT FILE1.0 KB · Public source code for developers, search crawlers and AI agents
1package examples
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "io"
8 "net/http"
9 "strings"
10 "time"
11)
12
13// PostJSON sends a JSON body to a caller-configured test endpoint. Do not send
14// wallet secrets to arbitrary URLs. This is an educational HTTP primitive.
15func PostJSON(ctx context.Context, client *http.Client, url string, body []byte) ([]byte, error) {
16 if !strings.HasPrefix(url, "http://127.0.0.1:") && !strings.HasPrefix(url, "http://localhost:") {
17 return nil, errors.New("example client allows loopback only")
18 }
19 if client == nil {
20 client = &http.Client{Timeout: 5 * time.Second}
21 }
22 req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
23 if err != nil {
24 return nil, err
25 }
26 req.Header.Set("Content-Type", "application/json")
27 resp, err := client.Do(req)
28 if err != nil {
29 return nil, err
30 }
31 defer resp.Body.Close()
32 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
33 return nil, errors.New("mock engine returned non-2xx")
34 }
35 return io.ReadAll(io.LimitReader(resp.Body, 1<<20))
36}Read-only source · Select a line number to link directly · Raw download above
