fish2018/pansou · error

create request failed

Error message

create request failed: %w

What it means

searchPage builds the POST request with http.NewRequestWithContext targeting MelostSearchAPI; if NewRequestWithContext fails (e.g. invalid URL), the error is wrapped as "create request failed: %w". This indicates the configured MelostSearchAPI URL is malformed or the context/method arguments are invalid.

Solutions

  1. Read the wrapped error; net/url parse errors identify the malformed part of the URL
  2. Print/inspect the MelostSearchAPI constant and fix the URL (add scheme, escape special characters)
  3. If the URL comes from configuration, validate it with url.Parse before constructing the request
  4. Verify no control characters or whitespace leaked into the endpoint string

Example fix

// before: constant typo breaks NewRequest
const MelostSearchAPI = "htp://www.melost.cn/api/search"
// after
const MelostSearchAPI = "https://www.melost.cn/api/search"
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(MelostSearchAPI)
if err != nil || u.Scheme == "" || u.Host == "" {
	return nil, fmt.Errorf("invalid MelostSearchAPI URL: %q", MelostSearchAPI)
}

Try / catch

req, err := http.NewRequestWithContext(ctx, "POST", MelostSearchAPI, bytes.NewBuffer(jsonData))
if err != nil {
	return nil, fmt.Errorf("create request failed: %w", err)
}

Prevention

When it happens

Trigger: MelostSearchAPI constant contains an unparsable URL (missing scheme, illegal characters, control characters), or a non-nil invalid context is passed to NewRequestWithContext.

Common situations: A bad constant edit after a site migration (URL typo, missing https://), or MelostSearchAPI read from user config with an invalid value.

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 fish2018/pansou@beaa561337 (2026-09-07). Data as JSON: /api/errors/c10dca3ac7a4bd60. Report an issue: GitHub.

Appendix: source

Thrown at plugin/melost/melost.go:160

			"wechat_pwd":  "",
			"search_code": "",
			"platform":    "pc",
			"fp_data":     "",
			"automated":   DefaultAutomated,
		},
	}

	jsonData, err := json.Marshal(reqBody)
	if err != nil {
		return nil, fmt.Errorf("marshal request failed: %w", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), DefaultTimeout)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, "POST", MelostSearchAPI, bytes.NewBuffer(jsonData))
	if err != nil {
		return nil, fmt.Errorf("create request failed: %w", err)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json, text/plain, */*")
	req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
	req.Header.Set("Origin", "https://www.melost.cn")
	req.Header.Set("Referer", DefaultReferer)
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36")

	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("request failed: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
	}

View on GitHub (pinned to beaa561337)