xpzouying/xiaohongshu-mcp · error
invalid image URL format
Error message
invalid image URL format
What it means
DownloadImage validates the input URL with isValidImageURL before issuing the HTTP GET; if the URL fails that check it returns 'invalid image URL format' with an empty path. This is a fail-fast guard so the downloader never sends requests to malformed or non-image-looking URLs.
Source
Thrown at pkg/downloader/images.go:44
// 确保保存目录存在
if err := os.MkdirAll(savePath, 0755); err != nil {
panic(fmt.Sprintf("failed to create save path: %v", err))
}
return &ImageDownloader{
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))
}
// 下载图片数据View on GitHub (pinned to 332d196854)
Solutions
- Print the offending URL and fix the extraction so full https CDN URLs are captured (prepend https://xcx.xiaohongshu.com for relative paths)
- Pre-validate the URL in your pipeline before calling DownloadImage
- Skip invalid URLs and continue the batch instead of aborting DownloadImages
- If placeholder/1x1 URLs are the issue, wait for lazy-load images to hydrate before reading src
Example fix
// before
path, err := downloader.DownloadImage(imgSrc)
if err != nil { return err }
// after
if !strings.HasPrefix(imgSrc, "http") {
imgSrc = "https://xcx.xiaohongshu.com" + imgSrc
}
path, err := downloader.DownloadImage(imgSrc)
if err != nil { log.Printf("skip image %q: %v", imgSrc, err); continue } Defensive patterns
Strategy: validation
Validate before calling
func validImageURL(u string) bool {
p, err := url.Parse(u)
return err == nil && (p.Scheme == "http" || p.Scheme == "https") && p.Host != ""
}
if !validImageURL(imgSrc) { /* skip */ } Type guard
func isDownloadableImageURL(s string) bool {
u, err := url.Parse(s)
return err == nil && u.IsAbs() && u.Host != ""
} Try / catch
path, err := downloader.DownloadImage(imgSrc)
if err != nil && strings.Contains(err.Error(), "invalid image URL format") {
log.Printf("skipping bad URL %q", imgSrc)
continue
} Prevention
- Normalize relative image paths to absolute CDN URLs before download
- Guard against lazy-load placeholders by waiting for image hydration
- Validate URLs at extraction time, not download time
When it happens
Trigger: Calling ImageDownloader.DownloadImage (pkg/downloader/images.go:44) with an empty string, a URL missing http/https scheme, a non-image extension, or garbage text that came from bad extraction of the image src attribute.
Common situations: Scraper captured a lazy-load placeholder or data URI instead of the real URL; note detail extraction returned relative paths without the CDN host; config file contains a mistyped image host; upstream API changed image URL shape.
Understand the failure class
Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.
Related errors
- downloaded file is not a valid image
- 图片不能为空
- 视频不能为空
- 下载内置浏览器失败: %w 本项目只用内置浏览器,缺它不继续。请检查网络后重试; 离线环境可手动下载 %s,解压
- 校验失败: %w
AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05).
Data as JSON: /api/errors/71d0dd7b0584b2c1.
Report an issue: GitHub.