jeessy2/ddns-go · error

创建请求失败: %v

Error message

创建请求失败: %v

What it means

After building the signed URL, Nowcn.request calls http.NewRequest; any failure constructing the request (most commonly an invalid method or an unparseable URL) is wrapped as '创建请求失败' (request creation failed). Nothing was sent to the provider.

Source

Thrown at dns/nowcn.go:251

	return strings.Join(finalQuery, "&"), nil
}

func (t *Nowcn) request(apiPath string, params map[string]string, method string) ([]byte, error) {
	// 生成签名
	queryString, err := t.sign(params, method)
	if err != nil {
		return nil, fmt.Errorf("生成签名失败: %v", err)
	}

	// 构造完整URL
	baseURL := "https://api.now.cn"
	fullURL := baseURL + apiPath + "?" + queryString

	// 创建HTTP请求
	req, err := http.NewRequest(method, fullURL, nil)
	if err != nil {
		return nil, fmt.Errorf("创建请求失败: %v", err)
	}

	// 设置请求头
	req.Header.Set("Accept", "application/json")

	// 发送请求
	client := t.httpClient
	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("请求失败: %v", err)
	}
	defer resp.Body.Close()

	// 读取响应
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("读取响应失败: %v", err)
	}

View on GitHub (pinned to 5874c2e666)

Solutions

  1. Print fullURL before http.NewRequest to inspect it
  2. URL-encode all param values (url.Values.Encode already escapes; verify sign output is escaped)
  3. Verify the method argument is exactly GET/POST/PUT/DELETE
  4. Ensure apiPath starts with '/' and contains no invalid characters

Example fix

// before
req, err := http.NewRequest(method, fullURL, nil)
if err != nil {
    return nil, fmt.Errorf("创建请求失败: %v", err)
}
// after
req, err := http.NewRequest(method, fullURL, nil)
if err != nil {
    return nil, fmt.Errorf("创建请求失败 (url=%s, method=%s): %w", fullURL, method, err)
}
Defensive patterns

Strategy: validation

Validate before calling

// Go: ensure params are encoded and method valid before request
if method != http.MethodGet && method != http.MethodPost {
    return fmt.Errorf("unsupported method: %s", method)
}
// ensure queryString comes from url.Values.Encode() so all values are escaped

Type guard

func validURL(u string) bool {
    parsed, err := url.Parse(u)
    return err == nil && parsed.Scheme == "https" && parsed.Host != ""
}

Try / catch

body, err := nowcnClient.request(apiPath, params, method)
if err != nil {
    if strings.Contains(err.Error(), "创建请求失败") {
        return fmt.Errorf("malformed request (check params/method): %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: http.NewRequest errors — malformed fullURL (empty queryString producing a bad URL, control characters in params), or an invalid HTTP method string passed to request().

Common situations: Param values containing unencoded special characters/spaces that break the URL, apiPath typo producing an invalid URL, or a refactor passing a wrong method constant.

Related errors


AI-assisted analysis of jeessy2/ddns-go@5874c2e666 (2026-09-03). Data as JSON: /api/errors/47ce0bd3ea2cb9ce. Report an issue: GitHub.