package examples import ( "bytes" "context" "errors" "io" "net/http" "strings" "time" ) // PostJSON sends a JSON body to a caller-configured test endpoint. Do not send // wallet secrets to arbitrary URLs. This is an educational HTTP primitive. func PostJSON(ctx context.Context, client *http.Client, url string, body []byte) ([]byte, error) { if !strings.HasPrefix(url, "http://127.0.0.1:") && !strings.HasPrefix(url, "http://localhost:") { return nil, errors.New("example client allows loopback only") } if client == nil { client = &http.Client{Timeout: 5 * time.Second} } req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return nil, errors.New("mock engine returned non-2xx") } return io.ReadAll(io.LimitReader(resp.Body, 1<<20)) }