chenhg5/cc-connect · info
create request: %w
Error message
create request: %w
What it means
This error wraps a failure from http.NewRequestWithContext when building the POST to https://api.dingtalk.com/v1.0/card/instances/createAndDeliver (platform/dingtalk/card.go:121, createAICard). NewRequest only errors on an invalid method, malformed URL, or a nil/invalid body reader, so with the hardcoded POST and constant URL this is effectively unreachable and signals a programming/environmental invariant break.
Source
Thrown at platform/dingtalk/card.go:121
payload["imRobotOpenDeliverModel"] = map[string]any{
"spaceType": "IM_ROBOT",
"robotCode": p.robotCode,
}
}
bodyBytes, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("marshal payload: %w", err)
}
reqCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(reqCtx, http.MethodPost,
"https://api.dingtalk.com/v1.0/card/instances/createAndDeliver",
bytes.NewReader(bodyBytes))
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-acs-dingtalk-access-token", token)
slog.Debug("dingtalk: creating AI card", "outTrackId", outTrackId, "isGroup", isGroup)
resp, err := p.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
slog.Debug("dingtalk: createAndDeliver response",
"status", resp.StatusCode,
"body", string(respBody))View on GitHub (pinned to 4000b2338a)
Solutions
- Verify the URL constant is unmodified and contains no spaces or invalid characters (url.Parse the value manually to see the exact error)
- If the URL is now configurable, validate it with url.Parse and http.CanonicalHeaderKey-style checks before constructing the request
- Confirm the HTTP method string is exactly "POST" with no whitespace
- If untouched stock code, report as a bug including the wrapped error
Example fix
// before (interpolated, unvalidated URL)
url := cfg.CardURL // may be empty or malformed
// after
u, err := url.Parse(cfg.CardURL)
if err != nil || u.Scheme == "" || u.Host == "" { return nil, fmt.Errorf("invalid card url: %w", err) } Defensive patterns
Strategy: validation
Validate before calling
u, err := url.Parse("https://api.dingtalk.com/v1.0/card/instances/createAndDeliver")
if err != nil || u.Scheme != "https" || u.Host == "" {
// fail fast before building request
} Try / catch
card, err := p.CreateStreamingCard(ctx, rc)
if err != nil && strings.Contains(err.Error(), "create request:") {
log.Printf("request construction invariant broken: %v", err)
} Prevention
- Keep API URLs as compile-time constants
- Validate any externalized URL with url.Parse before use
- Never build method strings from user/config input
When it happens
Trigger: http.NewRequestWithContext rejects the hardcoded method POST, the constant URL https://api.dingtalk.com/v1.0/card/instances/createAndDeliver, or the bytes.NewReader(bodyBytes) body; bodyBytes is always non-nil so this practically cannot fire in stock code.
Common situations: Source modifications that parameterize the URL with an invalid value (spaces, control characters, bad scheme) or change the HTTP method to an invalid token; corporate proxies never cause this (they fail later at do request).
Understand the failure class
Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.
Related errors
- dingtalk: create proactive request: %w
- request usage endpoint: %w
- usage endpoint returned status %d: %s
- decode usage response: %w
- fetch presets: %w
AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06).
Data as JSON: /api/errors/a561217f1254d1fe.
Report an issue: GitHub.