sindresorhus/got · error · TypeError

To get a Uint8Array, set `options.responseType` to `buffer`

Error message

To get a Uint8Array, set `options.responseType` to `buffer` instead

What it means

Thrown by the `encoding` setter when it is set to `null`. In older Node HTTP APIs `encoding: null` meant "give me raw bytes", but Got now uses the separate `responseType` option for that. To avoid silent breakage the setter refuses `null` and points you at `responseType: 'buffer'`.

Source

Thrown at source/core/options.ts:3236

	}

	/**
	[Encoding](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) to be used on `setEncoding` of the response data.

	To get a [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), you need to set `responseType` to `buffer` instead.
	Don't set this option to `null`.

	__Note__: This doesn't affect streams! Instead, you need to do `got.stream(...).setEncoding(encoding)`.

	@default 'utf-8'
	*/
	get encoding(): BufferEncoding | undefined {
		return this.#internals.encoding;
	}

	set encoding(value: BufferEncoding | undefined) {
		if (value === null) {
			throw new TypeError('To get a Uint8Array, set `options.responseType` to `buffer` instead');
		}

		assertAny('encoding', [is.string, is.undefined], value);

		this.#internals.encoding = value;
	}

	/**
	When set to `true` the promise will return the Response body instead of the Response object.

	@default false
	*/
	get resolveBodyOnly(): boolean {
		return this.#internals.resolveBodyOnly;
	}

	set resolveBodyOnly(value: boolean) {
		assert.boolean(value);

View on GitHub (pinned to e3924aa1e5)

Solutions

  1. Set `responseType: 'buffer'` to receive a `Uint8Array` body.
  2. Use a valid `BufferEncoding` string (e.g. `'utf-8'`, `'base64'`) if you actually want text decoding.
  3. Remove any `encoding: null` from your config.

Example fix

// before
const body = await got(url, {encoding: null}).body;
// after
const body = await got(url, {responseType: 'buffer'}).body;
Defensive patterns

Strategy: validation

Validate before calling

function normalizeEncoding(options) {
  if (options && options.encoding === null) {
    options.responseType = 'buffer';
    delete options.encoding;
  }
  return options;
}

Prevention

When it happens

Trigger: Calling `got(url, {encoding: null})` to try to get raw bytes, or any assignment of `null` to `options.encoding` (including inside a hook).

Common situations: Migrating from `request` (which used `encoding: null` for buffers) or raw `http`; following an outdated tutorial; unconditionally spreading a config that sets encoding to null.

Related errors


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