{"record":{"id":"cc5bfbae62a7a9b9","repo":"Mintplex-Labs/anything-llm","slug":"the-request-body-of-this-api-call-block-is-not-valid-json","errorCode":null,"errorMessage":"The request body of this API Call block is not valid JSON. Open the flow in the Agent Builder and check the body for syntax errors such as missing commas, brackets, or quotes.","messagePattern":"The request body of this API Call block is not valid JSON\\. Open the flow in the Agent Builder and check the body for syntax errors such as missing commas, brackets, or quotes\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/agentFlows/executors/api-call.js","lineNumber":32,"sourceCode":"\n  const requestConfig = {\n    method,\n    headers: headers.reduce((acc, h) => ({ ...acc, [h.key]: h.value }), {}),\n  };\n\n  if ([\"POST\", \"PUT\", \"PATCH\"].includes(method)) {\n    if (bodyType === \"form\") {\n      const formDataObj = new URLSearchParams();\n      formData.forEach(({ key, value }) => formDataObj.append(key, value));\n      requestConfig.body = formDataObj.toString();\n      requestConfig.headers[\"Content-Type\"] =\n        \"application/x-www-form-urlencoded\";\n    } else if (bodyType === \"json\") {\n      const parsedBody = safeJsonParse(body, null);\n      if (parsedBody !== null) {\n        requestConfig.body = JSON.stringify(parsedBody);\n      } else if (body?.trim()) {\n        throw new Error(\n          \"The request body of this API Call block is not valid JSON. Open the flow in the Agent Builder and check the body for syntax errors such as missing commas, brackets, or quotes.\"\n        );\n      }\n      requestConfig.headers[\"Content-Type\"] = \"application/json\";\n    } else if (bodyType === \"text\") {\n      requestConfig.body = String(body);\n    } else {\n      requestConfig.body = body;\n    }\n  }\n\n  try {\n    introspect(`Sending body to ${url}: ${requestConfig?.body || \"No body\"}`);\n    const response = await fetch(url, requestConfig);\n    if (!response.ok) {\n      introspect(`Request failed with status ${response.status}`);\n      throw new Error(`HTTP error! status: ${response.status}`);\n    }","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/f92433b4ea0598492a1e6645ea22addbb4dd1287/server/utils/agentFlows/executors/api-call.js#L14-L50","documentation":"The Agent Flow API Call block validates that a JSON-typed request body actually parses as JSON before sending. When safeJsonParse returns null and the body is non-empty, the block aborts with this instructional message telling the flow author to fix the JSON syntax in the Agent Builder.","triggerScenarios":"An agent flow with an API Call block whose bodyType is 'json' and whose body contains malformed JSON — trailing commas, unquoted keys, single quotes, unescaped newlines, or a template placeholder left unresolved producing invalid JSON.","commonSituations":"Hand-editing flow JSON and introducing syntax errors; pasting a body from curl with shell-style quotes; using {{variable}} placeholders that expand to non-JSON values; copying JSON with comments into the body field.","solutions":["Open the flow in the Agent Builder and paste the body into a JSON linter to find the syntax error.","Remove trailing commas, single quotes, and comments from the body.","If the body contains variables, switch bodyType to 'text'/'form' or ensure the substituted values are valid JSON.","Validate the resolved body (after variable substitution) not just the literal template.","Re-run the flow after fixing; the error is deterministic for the same body."],"exampleFix":"// before (body)\n{\"name\": 'test',}\n// after\n{\"name\": \"test\"}","handlingStrategy":"validation","validationCode":"function isValidJsonBody(body) {\n  if (!body || !body.trim()) return true; // empty is allowed\n  try { JSON.parse(body); return true; } catch { return false; }\n}\n// call before triggering the flow\nif (!isValidJsonBody(block.body)) throw new Error('API Call block body is not valid JSON');","typeGuard":"function isJsonObject(v) {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  await runAgentFlow(flowId, vars);\n} catch (e) {\n  if (/API Call block is not valid JSON/.test(e.message)) {\n    // surface a 400 to the flow author with the block name\n    return res.status(400).json({ error: 'Fix the API Call block body in the Agent Builder' });\n  }\n  throw e;\n}","preventionTips":["Validate flow definitions with a JSON-schema linter at save time in the Agent Builder.","Reject trailing commas, comments, and single quotes when editing bodies.","Substitute variables before assuming the body is valid JSON.","Use 'text' bodyType when the payload is not strict JSON."],"tags":["json","agent-flow","validation","config"],"backgroundTag":"json-parse-error","analyzedSha":"f92433b4ea0598492a1e6645ea22addbb4dd1287","analyzedAt":"2026-09-22T02:13:38.154Z","contentChangedAt":"2026-09-22T02:13:38.154Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}