langfuse/langfuse · error

Invalid JSON body

Error message

Invalid JSON body

What it means

Returned when the PATCH body arrives as a string that fails JSON.parse. Next.js usually parses JSON bodies automatically, so this fires when the body is malformed JSON or the client sends a string content type.

Source

Thrown at web/src/pages/api/public/scim/Users/[id].ts:535

// PATCH - Update user details (Use only for deprovisioning for now)
// Payload is a string like: "{\"schemas\":[\"urn:ietf:params:scim:api:messages:2.0:PatchOp\"],\"Operations\":[{\"op\":\"replace\",\"value\":{\"active\":false}}]}"
async function handlePatch(
  req: NextApiRequest,
  res: NextApiResponse,
  user: User,
  orgId: string,
  apiKeyId: string,
) {
  let body = req.body;

  // Check if body is a string and parse it
  if (typeof body === "string") {
    try {
      body = JSON.parse(body);
    } catch (error) {
      logger.warn("[SCIM] Failed to parse JSON body", error);
      return res.status(400).json({
        schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
        detail: "Invalid JSON body",
        status: 400,
      });
    }
  }

  // Validate the request body
  if (
    !body.schemas ||
    !Array.isArray(body.schemas) ||
    !body.schemas.includes("urn:ietf:params:scim:api:messages:2.0:PatchOp")
  ) {
    logger.warn(
      "[SCIM] Invalid request body. Must include 'schemas' with 'urn:ietf:params:scim:api:messages:2.0:PatchOp'.",
      body,
    );
    return res.status(400).json({

View on GitHub (pinned to 59d92c7cf3)

Solutions

  1. Validate the request body with a JSON linter before sending
  2. Ensure Content-Type: application/json is set and the payload is a single valid JSON object
  3. Capture the exact request (e.g. requestbin) to see what the IdP actually transmits

Example fix

// before (invalid)
curl -X PATCH .../Users/123 -d "{active: true}"  // unquoted key
// after
curl -X PATCH .../Users/123 -H 'Content-Type: application/json' -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"replace","value":{"active":true}}]}'
Defensive patterns

Strategy: validation

Validate before calling

try { JSON.parse(rawBody); } catch { throw new Error('Body is not valid JSON — fix before sending'); }

Type guard

const isParsableJson = (s: string) => { try { JSON.parse(s); return true; } catch { return false; } };

Try / catch

Catch the 400 and surface the raw payload to whoever generated it; do not retry unchanged.

Prevention

When it happens

Trigger: PATCH /api/public/scim/Users/{id} with a syntactically invalid JSON body (trailing commas, truncated payload, wrong quotes).

Common situations: Hand-crafted curl requests with broken quoting; proxies or IdP connectors mangling the payload; sending form-encoded data where JSON is expected.

Understand the failure class

Related errors


AI-assisted analysis of langfuse/langfuse@59d92c7cf3 (2026-08-27). Data as JSON: /api/errors/80e4855b372cfb2c. Report an issue: GitHub.