jeessy2/ddns-go · error

创建请求失败: %v

Error message

创建请求失败: %v

What it means

dns/tnethk.go request() wraps http.NewRequest failures with "创建请求失败: %v" (failed to create request). This happens before any network traffic: the method/URL pair is invalid, e.g. a malformed fullURL built from apiPath or the signed query string.

Source

Thrown at dns/tnethk.go:262

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

func (t *Tnethk) 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://www.tnet.hk"
	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. Inspect the wrapped %v error for the URL parse failure detail
  2. Ensure domain/subdomain and record values are URL-escaped before being placed in params
  3. Validate that apiPath and configured domain values contain no spaces or control characters
  4. Sanitize config inputs (trim whitespace, reject invalid hostname characters)

Example fix

// before
params["domain"] = domain.DomainName // may contain unsafe chars
// after
params["domain"] = url.QueryEscape(strings.TrimSpace(domain.DomainName))
Defensive patterns

Strategy: validation

Validate before calling

// sanitize inputs that end up in the URL
name := strings.TrimSpace(domain.SubDomain)
if strings.ContainsAny(name, " \t\r\n") {
	return fmt.Errorf("invalid subdomain %q", name)
}

Type guard

func validHost(s string) bool {
	return s != "" && net.ParseIP(s) != nil ||
		regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$`).MatchString(s)
}

Try / catch

if err != nil && strings.Contains(err.Error(), "创建请求失败") {
	return fmt.Errorf("tnethk: malformed request URL — check domain/param values: %w", err)
}

Prevention

When it happens

Trigger: create, modify, or getRecordList calls where baseURL+apiPath+"?"+queryString does not parse as a valid URL — e.g. apiPath with spaces/invalid characters or unescaped special characters in signed params.

Common situations: Domain or record names containing characters that were not URL-encoded before signing; apiPath typo producing an invalid URL; control characters leaking from config values into parameters.

Related errors


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