fish2018/pansou · error

创建GET请求失败

Error message

创建GET请求失败: %w

What it means

getFormhash wraps a failure from http.NewRequestWithContext when building the GET request for the site homepage used to extract formhash. This means the request object could not be constructed before any network I/O.

Solutions

  1. Check the BaseURL constant is a valid absolute HTTP(S) URL
  2. Print/log BaseURL to spot stray whitespace or invalid characters
  3. Fix the URL value; this is a compile-time/config issue, not a runtime network issue

Example fix

// before
const BaseURL = "http://example.com base"
// after
const BaseURL = "http://example.com"
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(BaseURL)
if err != nil || u.Scheme == "" || u.Host == "" {
    // BaseURL is malformed; fix configuration before calling Search
}

Type guard

func isValidURL(s string) bool {
    u, err := url.Parse(s)
    return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
}

Prevention

When it happens

Trigger: http.NewRequestWithContext(ctx, "GET", BaseURL, nil) errors, which happens only when the method or URL is invalid — e.g., BaseURL fails url.Parse (malformed URL, control characters).

Common situations: BaseURL constant corrupted by an edit or bad build-time substitution, or accidentally containing whitespace/newlines/invalid characters.

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/8b48b629f53024b5. Report an issue: GitHub.

Appendix: source

Thrown at plugin/qupanshe/qupanshe.go:147

		Transport: baseClient.Transport,
		Jar:       jar, // ⭐ 关键:添加Cookie管理
	}

	if DebugLog {
		fmt.Printf("[qupanshe] 创建带Cookie管理的session客户端,超时时间: %v\n", sessionClient.Timeout)
	}

	return sessionClient, nil
}

// getFormhash 从首页获取真实的formhash值
func (p *QupanshePlugin) getFormhash(client *http.Client) (string, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, "GET", BaseURL, nil)
	if err != nil {
		return "", fmt.Errorf("创建GET请求失败: %w", err)
	}

	p.setRequestHeaders(req)

	if DebugLog {
		fmt.Printf("[qupanshe] 请求首页获取formhash: %s\n", BaseURL)
	}

	resp, err := client.Do(req)
	if err != nil {
		return "", fmt.Errorf("GET请求失败: %w", err)
	}
	defer resp.Body.Close()

	// 调试:显示从首页获取的cookies
	if DebugLog && client.Jar != nil {
		if u, _ := url.Parse(BaseURL); u != nil {
			cookies := client.Jar.Cookies(u)

View on GitHub (pinned to beaa561337)