sindresorhus/got · error · ParseError

ERR_BODY_PARSE_FAILURE

ERR_BODY_PARSE_FAILURE

Error message

Unknown body type '${responseType as string}'

What it means

From `parseBody` in response.ts:187: Got supports only `text`, `json`, and `buffer` response types. If `responseType` is anything else (misspelled, an unknown string, or a value leaked from a custom option), the function falls through all branches and throws a `ParseError` with code `ERR_BODY_PARSE_FAILURE` and the offending type in the message. The error carries the `response` so callers can inspect it.

Source

Thrown at source/core/response.ts:187

		}

		if (responseType === 'json') {
			if (rawBody.length === 0) {
				return '';
			}

			const text = cachedDecodedBody ?? decodeUint8Array(rawBody, encoding);
			return parseJson(text);
		}

		if (responseType === 'buffer') {
			return rawBody;
		}
	} catch (error) {
		throw new ParseError(error as Error, response);
	}

	throw new ParseError({
		message: `Unknown body type '${responseType as string}'`,
		name: 'Error',
	}, response);
};

View on GitHub (pinned to e3924aa1e5)

Solutions

  1. Set `responseType` to one of `'text' | 'json' | 'buffer'` (the only valid values).
  2. For streaming responses use `got.stream(...)` or `{isStream: true}`, not `responseType: 'stream'`.
  3. Type the option as `ResponseOptionType` (the library's union) so TypeScript rejects invalid values at compile time.
  4. Audit shared defaults/extends for stray `responseType` overrides.

Example fix

// before
const {body} = await got(url, {responseType: 'stream'});

// after
import got from 'got';
const stream = got.stream(url);
// or, for a buffered parse:
const {body} = await got(url, {responseType: 'json'});
Defensive patterns

Strategy: type-guard

Validate before calling

const VALID = new Set(['text', 'json', 'buffer']);
function safeResponseType(value) {
  return VALID.has(value) ? value : undefined;
}
const responseType = safeResponseType(myConfig.responseType);
if (!responseType) throw new Error('invalid responseType');
await got(url, {responseType});

Type guard

const isResponseType = (v: unknown): v is 'text' | 'json' | 'buffer' =>
  v === 'text' || v === 'json' || v === 'buffer';

Try / catch

try {
  const {body} = await got(url, {responseType});
} catch (error) {
  if (error.code === 'ERR_BODY_PARSE_FAILURE' && /Unknown body type/.test(error.message)) {
    // responseType was invalid; fix the option and retry
  } else throw error;
}

Prevention

When it happens

Trigger: Calling Got with `responseType: 'stream'` (not supported — that's the `isStream` option), `responseType: 'html'`, `responseType: undefined` after a bad merge, or a typo like `responseType: 'json '`. The throw happens during body decoding after the response is received.

Common situations: Confusing `responseType` (parse format) with `isStream` (transport mode); a shared defaults object that gets `responseType` overwritten by a typo; dynamically-built option objects where an undefined sneaks in; version confusion where docs mention an unavailable type.

Related errors


AI-assisted analysis of sindresorhus/got@e3924aa1e5 (2026-08-03). Data as JSON: /data/errors/6f43070ab2697a6c.json. Report an issue: GitHub.