Budibase/budibase · error

Failed to parse response body: ${err}

Error message

Failed to parse response body: ${err}

What it means

parseResponse attempts to parse the HTTP response body as JSON or XML based on the Content-Type header. If parsing throws and nothing had actually started parsing (triedParsing false), it rethrows as 'Failed to parse response body: <cause>'. This guards queries against bodies that claim a structured content type but are malformed or not parseable.

Source

Thrown at packages/server/src/integrations/rest.ts:336

        } else if (
          (hasContent && contentType.includes("text/xml")) ||
          contentType.includes("application/xml")
        ) {
          triedParsing = true
          let xmlResponse = await handleXml(responseTxt)
          data = xmlResponse.data as JSONValue
          raw = xmlResponse.rawXml
        } else {
          data = responseTxt
          raw = responseTxt
        }
      }
    } catch (err) {
      if (triedParsing) {
        data = responseTxt
        raw = responseTxt
      } else {
        throw new Error(`Failed to parse response body: ${err}`)
      }
    }

    const size = helpers.formatBytes(contentLength || "0")
    const time = `${Math.round(performance.now() - this.startTimeMs)}ms`
    // converts headers to plain object
    for (const [key, value] of response.headers.entries()) {
      headers[key] = value
    }

    // Check if a pagination cursor exists in the response
    let nextCursor: JSONValue | undefined
    if (pagination?.responseParam) {
      nextCursor = get(data, pagination.responseParam) as JSONValue | undefined
    }

    return {
      data,

View on GitHub (pinned to a81a902e9a)

Solutions

  1. Inspect the raw response body and its Content-Type header; fix the upstream to return valid content for the declared type
  2. If the endpoint returns HTML errors, fix auth/URL or handle the non-2xx status before parsing
  3. Point the query at an endpoint that returns well-formed JSON, or accept the plain-text fallback (bodies with non-JSON content types are returned as text)
  4. If the upstream truly mislabels content type, use a proxy/transform to correct the header or parse client-side
  5. Add retry logic for truncated responses from flaky upstreams

Example fix

// before: upstream returns HTML with Content-Type: application/json
Content-Type: application/json
<html>502 Bad Gateway</html>
// after: ensure upstream or proxy returns valid body for the type
Content-Type: application/json
{"ok": true}
Defensive patterns

Strategy: validation

Validate before calling

// validate body parses before handing to the query runner
const res = await fetch(url, opts)
const ct = res.headers.get("content-type") || ""
if (ct.includes("application/json")) {
  try { JSON.parse(await res.text()) } catch {
    throw new Error("Upstream returned non-JSON body with JSON content-type")
  }
}

Type guard

function isParseableJson(txt) {
  if (typeof txt !== "string") return false
  try { JSON.parse(txt); return true } catch { return false }
}

Try / catch

try {
  const result = await restQuery.execute()
} catch (e) {
  if (String(e.message).startsWith("Failed to parse response body:")) {
    // inspect raw body/status manually; treat as upstream content bug
  } else { throw e }
}

Prevention

When it happens

Trigger: A REST query whose response Content-Type is application/json (or xml) but whose body is invalid JSON/XML - e.g. an HTML error page or truncated body served with a JSON content-type header. Also thrown when reading response.text() itself fails before any parse attempt.

Common situations: API gateway/WAF returns an HTML 502/error page with Content-Type: application/json; response truncated mid-body; BOM or leading whitespace mixed with valid content in edge cases; upstream returns JSONP or single-quoted JSON mislabeled as application/json.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29). Data as JSON: /api/errors/ded2301f73c2595f. Report an issue: GitHub.