fix: set json content type for spacetime web api
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
2026-04-27 16:54:38 +08:00
parent 9aae7afb2e
commit 1e4a64f542
2 changed files with 10 additions and 3 deletions

View File

@@ -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 <web-api-token>`。这种情况下脚本不会自动授权;该 token 对应的 identity 必须已经是迁移操作员。
正式导入前建议先加 `--dry-run`,确认 JSON 可解析、版本匹配、表名都在迁移白名单内。

View File

@@ -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)}`,