fish2018/pansou · error

create request failed

Error message

create request failed: %w

What it means

After marshaling the body, doSearch creates the POST request to JikepanAPIURL via http.NewRequest. If the request cannot be constructed (malformed URL, invalid method/context), the error is wrapped as "create request failed".

Solutions

  1. Print/verify JikepanAPIURL is a valid absolute http(s) URL
  2. Use url.Parse on the constant to validate at startup
  3. Restore the correct default JikepanAPIURL constant
  4. Prefer http.NewRequestWithContext with a caller context for cancellation

Example fix

// before
req, err := http.NewRequest("POST", JikepanAPIURL, bytes.NewBuffer(jsonData))
if err != nil {
	return nil, fmt.Errorf("create request failed: %w", err)
}

// after
if u, perr := url.Parse(JikepanAPIURL); perr != nil || u.Scheme == "" || u.Host == "" {
	return nil, fmt.Errorf("invalid JikepanAPIURL: %q", JikepanAPIURL)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, JikepanAPIURL, bytes.NewBuffer(jsonData))
if err != nil {
	return nil, fmt.Errorf("create request failed: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

// Go
results, err := p.doSearch(ctx, keyword, ext)
if err != nil && strings.Contains(err.Error(), "create request failed") {
	log.Printf("jikepan endpoint misconfigured: %v", err)
}

Prevention

When it happens

Trigger: http.NewRequest("POST", JikepanAPIURL, ...) fails — typically because JikepanAPIURL is an empty string or not a parseable absolute URL (bad build-time constant or misconfigured endpoint).

Common situations: Endpoint constant mis-typed or stripped during refactoring; environment-specific config overriding the URL with an invalid value; missing scheme (e.g. "jikepan.xyz/search" without https://).

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/652eb5b0955278da. Report an issue: GitHub.

Appendix: source

Thrown at plugin/jikepan/jikepan.go:75

		"is_all": false,
	}
	
	// 检查ext中是否包含自定义参数,如果有则使用它
	if ext != nil {
		if isAll, ok := ext["is_all"].(bool); ok && isAll {
			// 使用全量搜索,时间大约10秒
			reqBody["is_all"] = true
		}
	}
	
	jsonData, err := json.Marshal(reqBody)
	if err != nil {
		return nil, fmt.Errorf("marshal request failed: %w", err)
	}
	
	req, err := http.NewRequest("POST", JikepanAPIURL, 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("referer", "https://jikepan.xyz/")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
	
	// 发送请求
	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("request failed: %w", err)
	}
	defer resp.Body.Close()
	
	// 解析响应
	var apiResp JikepanResponse
	bodyBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("read response body failed: %w", err)

View on GitHub (pinned to beaa561337)