xpzouying/xiaohongshu-mcp · error
failed to create request
Error message
failed to create request
What it means
DownloadImage wraps an http.NewRequest failure (pkg/downloader/images.go:50). Although isValidImageURL already rejects malformed URLs, http.NewRequest still parses the URL itself and can fail on control characters, invalid percent-escapes, or unsupported schemes that slipped past the prefix check. This is a client-side URL construction error — no network I/O has happened.
Source
Thrown at pkg/downloader/images.go:50
savePath: savePath,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// DownloadImage 下载图片
// 返回本地文件路径
func (d *ImageDownloader) DownloadImage(imageURL string) (string, error) {
// 验证URL格式
if !d.isValidImageURL(imageURL) {
return "", errors.New("invalid image URL format")
}
// 创建请求并设置请求头
req, err := http.NewRequest("GET", imageURL, nil)
if err != nil {
return "", errors.Wrap(err, "failed to create request")
}
// 设置 User-Agent,模拟浏览器请求
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
// 设置 Referer,使用图片 URL 的域名
parsedURL, _ := url.Parse(imageURL)
if parsedURL != nil {
req.Header.Set("Referer", fmt.Sprintf("%s://%s/", parsedURL.Scheme, parsedURL.Host))
}
// 下载图片数据
resp, err := d.httpClient.Do(req)
if err != nil {
return "", errors.Wrapf(err, "failed to download image from %s", imageURL)
}
defer resp.Body.Close()
View on GitHub (pinned to 332d196854)
Solutions
- Print/log the offending imageURL from the wrapped error and inspect for control characters or bad escapes.
- Trim whitespace and re-encode: url.Parse + u.String(), or strings.TrimSpace before calling.
- Validate with net/url.Parse yourself before invoking the downloader.
- Sanitize at ingestion time so bad URLs never reach DownloadImages batches.
Example fix
// before
path, err := downloader.DownloadImage(rawURL)
// after
cleaned := strings.TrimSpace(rawURL)
if _, err := url.Parse(cleaned); err != nil {
return fmt.Errorf("skip bad url %q: %w", cleaned, err)
}
path, err := downloader.DownloadImage(cleaned) Defensive patterns
Strategy: validation
Validate before calling
u, err := url.Parse(strings.TrimSpace(rawURL))
if err != nil || u.Scheme == "" || u.Host == "" {
return fmt.Errorf("invalid image url %q", rawURL)
} Try / catch
path, err := dl.DownloadImage(imgURL)
if err != nil && strings.Contains(err.Error(), "failed to create request") {
log.Printf("malformed url %q: %v", imgURL, err)
return nil // skip bad URL in batch
} Prevention
- Trim and re-encode scraped URLs (strip control chars/newlines).
- Validate with url.Parse before downloading.
- Sanitize URL sources at ingestion, not download time.
- In batches, skip-and-log invalid URLs rather than aborting.
When it happens
Trigger: Calling DownloadImage(url) or DownloadImages with a URL containing raw control characters (newline/tab from copied log lines), invalid percent-encoding like %zz, or a scheme http.NewRequest rejects, after passing the http(s)-prefix check.
Common situations: Image URLs scraped from HTML carrying embedded newlines or whitespace; unescaped characters from upstream data feeds; hand-built URLs with bad query encodings.
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
- 获取 SHA256SUMS: HTTP %d
- HTTP %d: %s
- download failed with status %d for URL: %s
- download errors occurred: %v
- 下载图片失败 %s: %w
AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05).
Data as JSON: /api/errors/dd3baf9bfd26da97.
Report an issue: GitHub.