Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cabca1

Browse files
chencheng-liclaude
andauthored
fix: use %20 instead of + for spaces in Bilibili WBI signed requests (#126)
URLSearchParams.toString() encodes spaces as +, but Bilibili's WBI signature verification expects %20. This mismatch causes search queries with spaces (e.g. "亚马逊 滞销产品") to fail with TypeError: Failed to fetch due to CORS-blocked error responses. Fixes #125 Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
1 parent fafa990 commit 4cabca1

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/bilibili.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export async function wbiSign(
6060
for (const key of Object.keys(allParams).sort()) {
6161
sorted[key] = String(allParams[key]).replace(/[!'()*]/g, '');
6262
}
63-
const query = new URLSearchParams(sorted).toString();
63+
// Bilibili WBI verification expects %20 for spaces, not + (URLSearchParams default).
64+
// Using + causes signature mismatch → CORS-blocked error response → TypeError: Failed to fetch.
65+
const query = new URLSearchParams(sorted).toString().replace(/\+/g, '%20');
6466
const wRid = await md5(query + mixinKey);
6567
sorted.w_rid = wRid;
6668
return sorted;
@@ -78,7 +80,7 @@ export async function apiGet(
7880
}
7981
const qs = new URLSearchParams(
8082
Object.fromEntries(Object.entries(params).map(([k, v]) => [k, String(v)])),
81-
);
83+
).toString().replace(/\+/g, '%20');
8284
const url = `${baseUrl}${path}?${qs}`;
8385
return fetchJson(page, url);
8486
}

0 commit comments

Comments
 (0)