chenhg5/cc-connect · error

create request: %w

Error message

create request: %w

What it means

getDownloadURL wraps http.NewRequestWithContext failure. Creating the POST request to https://api.dingtalk.com/v1.0/robot/messageFiles/download failed — almost always due to an invalid/malformed URL or a nil body reader; with this static, valid URL it should not occur at runtime.

Source

Thrown at platform/dingtalk/dingtalk.go:695

		return "", fmt.Errorf("get access token: %w", err)
	}

	reqBody := map[string]string{
		"downloadCode": downloadCode,
		"robotCode":    p.robotCode,
	}
	bodyBytes, err := json.Marshal(reqBody)
	if err != nil {
		return "", fmt.Errorf("marshal request: %w", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	req, err := http.NewRequestWithContext(ctx, http.MethodPost,
		"https://api.dingtalk.com/v1.0/robot/messageFiles/download",
		bytes.NewReader(bodyBytes))
	if err != nil {
		return "", fmt.Errorf("create request: %w", err)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("x-acs-dingtalk-access-token", token)

	resp, err := p.httpClient.Do(req)
	if err != nil {
		return "", fmt.Errorf("do request: %w", err)
	}
	defer func() { _ = resp.Body.Close() }()

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("api returned status %d", resp.StatusCode)
	}

	var result downloadResponse
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return "", fmt.Errorf("decode response: %w", err)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Verify the build is unmodified and the hardcoded endpoint URL is intact.
  2. If the URL was parameterized in a fork, validate/percent-encode it before passing to NewRequest.
  3. No config change prevents this; treat as an internal bug and report with the wrapped cause.
Defensive patterns

Strategy: try-catch

Validate before calling

// If the URL ever becomes configurable, pre-validate it:
if u, err := url.Parse(downloadAPIEndpoint); err != nil || u.Scheme != "https" || u.Host == "" {
    log.Fatal("invalid dingtalk download endpoint")
}

Try / catch

if err != nil && strings.Contains(err.Error(), "create request") {
    log.Error("dingtalk: malformed request construction (bug)", "err", err)
    return err
}

Prevention

When it happens

Trigger: http.NewRequestWithContext(http.MethodPost, "https://api.dingtalk.com/v1.0/robot/messageFiles/download", bytes.NewReader(bodyBytes)) returned an error.

Common situations: Essentially never in production with the hardcoded URL; would appear only if the code/build was altered (e.g. URL constructed from bad config) or the Go runtime/environment is corrupt.

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


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/f2c7a78fec358527. Report an issue: GitHub.