{"record":{"id":"8521f6e4e2655680","repo":"fish2018/pansou","slug":"post-w-8521f6","errorCode":null,"errorMessage":"创建POST请求失败: %w","messagePattern":"创建POST请求失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugin/qupanshe/qupanshe.go","lineNumber":235,"sourceCode":"// postSearchRequest 发送POST请求获取搜索结果URL\nfunc (p *QupanshePlugin) postSearchRequest(client *http.Client, keyword, formhash string) (string, error) {\n\t// 添加延时，避免请求过快\n\ttime.Sleep(2 * time.Second)\n\n\t// 构建POST请求\n\tsearchURL := fmt.Sprintf(\"%s/search.php?mod=forum\", BaseURL)\n\tdata := url.Values{}\n\tdata.Set(\"formhash\", formhash)\n\tdata.Set(\"srchtxt\", keyword)\n\tdata.Set(\"searchsubmit\", \"yes\")\n\n\tctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)\n\tdefer cancel()\n\n\tpostData := data.Encode()\n\treq, err := http.NewRequestWithContext(ctx, \"POST\", searchURL, strings.NewReader(postData))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"创建POST请求失败: %w\", err)\n\t}\n\n\tp.setRequestHeaders(req)\n\treq.Header.Set(\"Content-Type\", \"application/x-www-form-urlencoded\")\n\n\t// 详细日志：请求信息\n\tif DebugLog {\n\t\tfmt.Printf(\"[qupanshe] POST请求URL: %s\\n\", searchURL)\n\t\tfmt.Printf(\"[qupanshe] POST请求数据: %s\\n\", postData)\n\t\tfmt.Printf(\"[qupanshe] POST请求头:\\n\")\n\t\tfor key, values := range req.Header {\n\t\t\tfor _, value := range values {\n\t\t\t\tfmt.Printf(\"  %s: %s\\n\", key, value)\n\t\t\t}\n\t\t}\n\t\t\n\t\t// 显示将要发送的cookies\n\t\tif client.Jar != nil {","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/fish2018/pansou/blob/beaa56133755a548ebc51b090b3816e2ae044aa6/plugin/qupanshe/qupanshe.go#L217-L253","documentation":"postSearchRequest builds the http POST request with http.NewRequestWithContext and wraps any construction failure. Because the URL and body are assembled from earlier parsing, this almost always means the target URL was malformed (e.g. invalid characters from parsed form data) or the context was already invalid.","triggerScenarios":"searchURL passed to http.NewRequestWithContext fails URL parsing — typically when the BaseURL constant or the encoded form data contains characters that make an invalid URL (spaces, control chars, bad percent-encoding).","commonSituations":"BaseURL configured/edited incorrectly with typos or trailing whitespace; keyword contains characters that break url.Values encoding in an unexpected way; plugin version where BaseURL was changed to a bad value.","solutions":["Print/log the searchURL and postData just before http.NewRequestWithContext to spot malformed values","Validate searchURL with url.Parse before constructing the request","Fix the BaseURL constant to a clean, valid absolute URL (no spaces/BOM)","url.PathEscape/url.QueryEscape user-supplied keyword pieces","Ensure the encoded postData is passed via strings.NewReader with correct Content-Type"],"exampleFix":"// before\nreq, err := http.NewRequestWithContext(ctx, \"POST\", searchURL, strings.NewReader(postData))\nif err != nil {\n    return \"\", fmt.Errorf(\"创建POST请求失败: %w\", err)\n}\n// after\nif _, err := url.Parse(searchURL); err != nil {\n    return \"\", fmt.Errorf(\"invalid search URL %q: %w\", searchURL, err)\n}\nreq, err := http.NewRequestWithContext(ctx, \"POST\", searchURL, strings.NewReader(postData))\nif err != nil {\n    return \"\", fmt.Errorf(\"创建POST请求失败: %w\", err)\n}","handlingStrategy":"validation","validationCode":"u, err := url.Parse(searchURL)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid search URL: %q\", searchURL)\n}","typeGuard":null,"tryCatchPattern":"_, err := plugin.Search(ctx, keyword)\nvar urlErr *url.Error\nif errors.As(err, &urlErr) {\n    // malformed URL in request construction\n}","preventionTips":["Keep BaseURL a clean absolute URL constant; test it with url.Parse at startup","Escape user input with url.Values / url.QueryEscape before embedding","Log searchURL and postData in DebugLog builds","Add a unit test asserting the constructed URL parses"],"tags":["http","url","request-construction"],"backgroundTag":"invalid-url","analyzedSha":"beaa56133755a548ebc51b090b3816e2ae044aa6","analyzedAt":"2026-09-07T00:31:18.025Z","contentChangedAt":"2026-09-07T00:31:18.025Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}