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

  1. Provide at least one valid JSON value (object or array of objects) in the import body, e.g. one Codex auth.json document.
  2. If importing from a file, verify the file is non-empty and contains the expected exported auth.json content before uploading.
  3. 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

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


AI-assisted analysis of Wei-Shaw/sub2api@073e92d171 (2026-08-15). Data as JSON: /api/errors/e849d7d5c5e479d3. Report an issue: GitHub.