From 1e4a64f54202c7a43756f4671c07cd6d40ae6133 Mon Sep 17 00:00:00 2001 From: kdletters Date: Mon, 27 Apr 2026 16:54:38 +0800 Subject: [PATCH] fix: set json content type for spacetime web api --- ...MEDB_JSON_STRING_MIGRATION_PROCEDURE_2026-04-27.md | 2 ++ scripts/spacetime-migration-common.mjs | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/technical/SPACETIMEDB_JSON_STRING_MIGRATION_PROCEDURE_2026-04-27.md b/docs/technical/SPACETIMEDB_JSON_STRING_MIGRATION_PROCEDURE_2026-04-27.md index 6f1274a4..9dd88b5b 100644 --- a/docs/technical/SPACETIMEDB_JSON_STRING_MIGRATION_PROCEDURE_2026-04-27.md +++ b/docs/technical/SPACETIMEDB_JSON_STRING_MIGRATION_PROCEDURE_2026-04-27.md @@ -163,6 +163,8 @@ node scripts/spacetime-import-migration-json.mjs \ 3. 使用 `Authorization: Bearer <临时 token>` 调用 `import_database_migration_from_file`,把完整迁移 JSON 放在 HTTP body 中。 4. 导入请求结束后,脚本会用同一个临时 Web API token 调用 `revoke_database_migration_operator`,撤销该临时 identity。 +所有直接访问 SpacetimeDB Web API 的 POST 请求必须显式发送 `Content-Type: application/json`。部分 SpacetimeDB 版本不会接受省略 content type 或附带非预期 media type 的请求,即使 body 本身是合法 JSON,也会返回 `HTTP 415`。 + 如果你已经有可用的数据库连接 token,也可以显式传 `--token `。这种情况下脚本不会自动授权;该 token 对应的 identity 必须已经是迁移操作员。 正式导入前建议先加 `--dry-run`,确认 JSON 可解析、版本匹配、表名都在迁移白名单内。 diff --git a/scripts/spacetime-migration-common.mjs b/scripts/spacetime-migration-common.mjs index b65550c8..91e93e45 100644 --- a/scripts/spacetime-migration-common.mjs +++ b/scripts/spacetime-migration-common.mjs @@ -105,10 +105,11 @@ export async function callSpacetimeProcedure(options, procedureName, input) { const serverUrl = resolveServerUrl(options).replace(/\/+$/u, ''); const url = `${serverUrl}/v1/database/${encodeURIComponent(options.database)}/call/${encodeURIComponent(procedureName)}`; const headers = { - 'content-type': 'application/json; charset=utf-8', + Accept: 'application/json', + 'Content-Type': 'application/json', }; if (options.token) { - headers.authorization = `Bearer ${options.token}`; + headers.Authorization = `Bearer ${options.token}`; } let response; @@ -136,9 +137,13 @@ export async function callSpacetimeProcedure(options, procedureName, input) { export async function createSpacetimeWebIdentity(options) { const serverUrl = resolveServerUrl(options).replace(/\/+$/u, ''); const url = `${serverUrl}/v1/identity`; + const headers = { + Accept: 'application/json', + 'Content-Type': 'application/json', + }; let response; try { - response = await fetch(url, { method: 'POST' }); + response = await fetch(url, { method: 'POST', headers }); } catch (error) { throw new Error( `SpacetimeDB identity 请求失败: ${url}; ${error instanceof Error ? error.message : String(error)}`,