jeessy2/ddns-go · error
创建请求失败: %v
Error message
创建请求失败: %v
What it means
This error is thrown by Eranet.request when http.NewRequest fails to construct the outgoing HTTP request object for calls to create, modify, or getRecordList. Given the fullURL is already assembled from a constant baseURL, a signed queryString, and a method variable, failure almost always means an invalid HTTP method string or a malformed URL (e.g. unparsable characters in the query string). It wraps the underlying net/url parse error with %v.
Source
Thrown at dns/eranet.go:262
return strings.Join(finalQuery, "&"), nil
}
func (t *Eranet) 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.eranet.com"
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
- Trim and validate the API secret/key and all parameter values for whitespace or control characters before calling create/modify
- Verify the method argument is exactly GET or POST (uppercase, no spaces)
- Print/inspect the fullURL (it is baseURL+apiPath+"?"+queryString) to spot malformed characters
- Check util.PercentEncode output for the offending parameter
Example fix
// before
req, err := http.NewRequest(method, fullURL, nil)
// after
method = strings.ToUpper(strings.TrimSpace(method))
fullURL = strings.TrimSpace(fullURL)
req, err := http.NewRequest(method, fullURL, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %v", err)
} Defensive patterns
Strategy: validation
Validate before calling
method = strings.ToUpper(strings.TrimSpace(method))
if method != http.MethodGet && method != http.MethodPost {
return fmt.Errorf("unsupported method: %q", method)
}
for k, v := range params {
if strings.ContainsAny(v, " \t\r\n\x00") {
return fmt.Errorf("parameter %q contains invalid characters", k)
}
} Type guard
func isValidMethod(m string) bool {
switch strings.ToUpper(strings.TrimSpace(m)) {
case http.MethodGet, http.MethodPost:
return true
}
return false
} Try / catch
body, err := eranetClient.CreateRecord(params)
if err != nil {
if strings.Contains(err.Error(), "创建请求失败") {
// request construction failed: method/URL malformed — fix inputs, no retry
return fmt.Errorf("eranet request malformed: %w", err)
}
return err
} Prevention
- Always pass plain uppercase GET/POST as the method
- Trim whitespace from API keys, secrets, and domain values read from config
- Never hand-build query strings with raw values; rely on the library's PercentEncode signing
- Log the full URL at debug level to catch encoding problems early
When it happens
Trigger: http.NewRequest returns an error when the method contains invalid characters or the fullURL fails url.Parse — e.g. a method other than a standard verb with bad characters, or a queryString containing raw spaces/control characters because signing/encoding (util.PercentEncode) produced an unexpected value.
Common situations: Misconfigured HTTP method passed down from create/modify; control characters or unencoded spaces in parameter values (API key/secret with trailing whitespace or newline) leaking into the query string; custom API paths containing characters illegal in URLs.
Related errors
AI-assisted analysis of jeessy2/ddns-go@5874c2e666 (2026-09-03).
Data as JSON: /api/errors/74a4376e0f660e81.
Report an issue: GitHub.