Wei-Shaw/sub2api · error
空 JSON 内容
Error message
空 JSON 内容
What it means
Returned by the Codex session-import JSON parser (backend/internal/handler/admin/account_codex_import.go:468) when the request body decodes successfully as JSON but contains zero top-level values — i.e. the stream is only whitespace or an empty document after json.Decoder loop hits io.EOF with nothing accumulated. It guards the admin import endpoint from creating an empty import batch.
Source
Thrown at backend/internal/handler/admin/account_codex_import.go:468
}
func decodeCodexJSONStream(content string) ([]any, error) {
decoder := json.NewDecoder(strings.NewReader(content))
decoder.UseNumber()
values := make([]any, 0, 1)
for {
var value any
err := decoder.Decode(&value)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
values = append(values, value)
}
if len(values) == 0 {
return nil, errors.New("空 JSON 内容")
}
return values, nil
}
func flattenCodexImportValues(values []any) []any {
out := make([]any, 0, len(values))
var appendValue func(any)
appendValue = func(value any) {
if arr, ok := value.([]any); ok {
for _, item := range arr {
appendValue(item)
}
return
}
out = append(out, value)
}
for _, value := range values {
appendValue(value)View on GitHub (pinned to 073e92d171)
Solutions
- Provide at least one valid JSON value (object or array of objects) in the import body, e.g. one Codex auth.json document.
- If importing from a file, verify the file is non-empty and contains the expected exported auth.json content before uploading.
- In the admin UI, add a client-side check that rejects empty/whitespace-only input before submitting.
Example fix
// before
curl -X POST /admin/codex/import -H 'Content-Type: application/json' -d ''
// after
curl -X POST /admin/codex/import -H 'Content-Type: application/json' -d '{"access_token":"...","refresh_token":"..."}' Defensive patterns
Strategy: validation
Validate before calling
function hasJsonContent(text: string): boolean {
return JSON.parse(text) !== undefined && text.trim() !== ''
}
// Go side, before calling the decoder:
if strings.TrimSpace(string(body)) == "" {
return errors.New("empty payload")
} Try / catch
values, err := decodeCodexValues(body)
if err != nil {
if err.Error() == "空 JSON 内容" {
http.Error(w, "upload a non-empty JSON export", http.StatusBadRequest)
return
}
http.Error(w, err.Error(), http.StatusBadRequest)
} Prevention
- Client-side: reject empty or whitespace-only files/text before POSTing the import.
- Check the selected file's size > 0 in the admin UI file picker.
- After exporting, sanity-check the artifact contains at least one JSON object before re-importing.
When it happens
Trigger: POSTing an import payload that is an empty string, only whitespace/newlines, or comments-only JSON; uploading an empty .json file selected by mistake; a client that serializes an empty array/object incorrectly and sends an empty body with Content-Type application/json.
Common situations: Admin UI file-picker allowing zero-byte files; curl with an empty -d '' argument; automated export that wrote no records and the operator re-imports the empty artifact; clipboard paste of nothing into the import textarea.
Related errors
- agent identity 缺少必要字段
- agent identity private key 格式无效
- 缺少 accessToken/access_token
- 未包含 refresh_token,且无法解析 accessToken 过期时间;请在第一步设置过期时间后再导入
- proxies is required
AI-assisted analysis of Wei-Shaw/sub2api@073e92d171 (2026-08-15).
Data as JSON: /api/errors/e849d7d5c5e479d3.
Report an issue: GitHub.