NativeScript/NativeScript · error · Error

could not read FormData body as text

Error message

could not read FormData body as text

What it means

Same Body restriction as blob(): text() cannot represent a FormData body, so when _bodyFormData is set, text() throws Error "could not read FormData body as text". FormData must be consumed via formData() or serialized explicitly.

Source

Thrown at packages/core/fetch/index.mjs:298

                    return consumed(this) || Promise.resolve(this._bodyArrayBuffer);
                } else {
                    return this.blob().then(readBlobAsArrayBuffer);
                }
            };
        }

        this.text = function () {
            var rejected = consumed(this);
            if (rejected) {
                return rejected;
            }

            if (this._bodyBlob) {
                return readBlobAsText(this._bodyBlob);
            } else if (this._bodyArrayBuffer) {
                return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer));
            } else if (this._bodyFormData) {
                throw new Error("could not read FormData body as text");
            } else {
                return Promise.resolve(this._bodyText);
            }
        };

        if (support.formData) {
            this.formData = function () {
                return this.text().then(decode);
            };
        }

        this.json = function () {
            return this.text().then(JSON.parse);
        };

        return this;
    }

View on GitHub (pinned to 6800aefa65)

Solutions

  1. Branch on body type: if (body instanceof FormData) use formData()/entries, else text().
  2. Serialize FormData manually (for (const [k,v] of form) ...) if text is truly needed.
  3. Catch the error and fall back to another reader in generic pipelines.

Example fix

// before
console.log(await response.text()); // throws for FormData body
// after
if (response.body instanceof FormData) {
  for (const [k, v] of response.body) console.log(k, v);
} else {
  console.log(await response.text());
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (response.body instanceof FormData) {
  // don't call text(); iterate formData() instead
}

Type guard

function isFormDataBody(body) {
  return typeof FormData !== 'undefined' && body instanceof FormData;
}

Try / catch

try {
  text = await response.text();
} catch (e) {
  if (e.message === 'could not read FormData body as text') {
    text = [...response.body].map(([k, v]) => `${k}=${v}`).join('&');
  } else throw e;
}

Prevention

When it happens

Trigger: Calling response.text() / request.text() on a body constructed from FormData — e.g. awaiting text() on an echo of a multipart request, or a logging layer that calls text() on every response.

Common situations: Logging interceptors that always call response.text(); unit tests constructing Response with FormData then reading text; generic download helpers ignoring body type.

Related errors


AI-assisted analysis of NativeScript/NativeScript@6800aefa65 (2026-08-30). Data as JSON: /api/errors/63e6c40acc2b533c. Report an issue: GitHub.