xpzouying/xiaohongshu-mcp · error

failed to unmarshal feeds: %w

Error message

failed to unmarshal feeds: %w

What it means

GetFeedsList 已从页面拿到 __INITIAL_STATE__.feed 的 JSON 字符串,但 json.Unmarshal 到 []Feed 失败:页面注水数据结构与 Feed 结构体定义不匹配(小红书前端改版)或数据被截断。

Source

Thrown at xiaohongshu/feeds.go:62

	}

	// 轮询等 __INITIAL_STATE__.feed 注水就绪(替代固定 1s,治偶发 ErrNoFeeds)
	var result string
	deadline := time.Now().Add(8 * time.Second)
	for {
		if result = readFeeds(); result != "" || time.Now().After(deadline) {
			break
		}
		time.Sleep(300 * time.Millisecond)
	}

	if result == "" {
		return nil, errors.ErrNoFeeds
	}

	var feeds []Feed
	if err := json.Unmarshal([]byte(result), &feeds); err != nil {
		return nil, fmt.Errorf("failed to unmarshal feeds: %w", err)
	}

	return onlyNotes(feeds), nil
}

View on GitHub (pinned to 332d196854)

Solutions

  1. Log the raw result and diff against the []Feed json tags
  2. Update the Feed struct tags to the current schema
  3. Ensure you are on the expected page (logged in, correct tab) before extraction

Example fix

// before
var feeds []Feed
if err := json.Unmarshal([]byte(result), &feeds); err != nil { return nil, ... }
// after
var probe any
if err := json.Unmarshal([]byte(result), &probe); err != nil { log.Printf("raw=%s", result) } // inspect shape first
Defensive patterns

Strategy: type-guard

Validate before calling

func looksLikeFeedArray(raw string) bool {
    var probe []json.RawMessage
    return json.Unmarshal([]byte(raw), &probe) == nil
}

Type guard

func asFeedSlice(raw string) ([]json.RawMessage, bool) {
    var s []json.RawMessage
    if err := json.Unmarshal([]byte(raw), &s); err != nil { return nil, false }
    return s, true
}

Try / catch

feeds, err := client.GetFeedsList(ctx)
if err != nil {
    if strings.Contains(err.Error(), "unmarshal feeds") {
        log.Errorf("feeds schema drift: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: The home/discover feed initial state changed shape (renamed fields, wrapped object instead of array) or the extraction returned an object/note-card HTML instead of a JSON array of feeds.

Common situations: xiaohongshu frontend update; extracting from a logged-out page where feeds list is absent and a different structure is returned; not-logged-in redirect rendering a different state.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/37966db1c68e09c2. Report an issue: GitHub.