{"id":"7857377a36dd9879","repo":"sindresorhus/got","slug":"to-get-a-uint8array-set-options-responsetype-to","errorCode":null,"errorMessage":"To get a Uint8Array, set `options.responseType` to `buffer` instead","messagePattern":"To get a Uint8Array, set `options\\.responseType` to `buffer` instead","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/core/options.ts","lineNumber":3236,"sourceCode":"\t}\n\n\t/**\n\t[Encoding](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) to be used on `setEncoding` of the response data.\n\n\tTo get a [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), you need to set `responseType` to `buffer` instead.\n\tDon't set this option to `null`.\n\n\t__Note__: This doesn't affect streams! Instead, you need to do `got.stream(...).setEncoding(encoding)`.\n\n\t@default 'utf-8'\n\t*/\n\tget encoding(): BufferEncoding | undefined {\n\t\treturn this.#internals.encoding;\n\t}\n\n\tset encoding(value: BufferEncoding | undefined) {\n\t\tif (value === null) {\n\t\t\tthrow new TypeError('To get a Uint8Array, set `options.responseType` to `buffer` instead');\n\t\t}\n\n\t\tassertAny('encoding', [is.string, is.undefined], value);\n\n\t\tthis.#internals.encoding = value;\n\t}\n\n\t/**\n\tWhen set to `true` the promise will return the Response body instead of the Response object.\n\n\t@default false\n\t*/\n\tget resolveBodyOnly(): boolean {\n\t\treturn this.#internals.resolveBodyOnly;\n\t}\n\n\tset resolveBodyOnly(value: boolean) {\n\t\tassert.boolean(value);","sourceCodeStart":3218,"sourceCodeEnd":3254,"githubUrl":"https://github.com/sindresorhus/got/blob/e3924aa1e53a6ca3eb93a43618ce532442a89b40/source/core/options.ts#L3218-L3254","documentation":"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'`.","triggerScenarios":"Calling `got(url, {encoding: null})` to try to get raw bytes, or any assignment of `null` to `options.encoding` (including inside a hook).","commonSituations":"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.","solutions":["Set `responseType: 'buffer'` to receive a `Uint8Array` body.","Use a valid `BufferEncoding` string (e.g. `'utf-8'`, `'base64'`) if you actually want text decoding.","Remove any `encoding: null` from your config."],"exampleFix":"// before\nconst body = await got(url, {encoding: null}).body;\n// after\nconst body = await got(url, {responseType: 'buffer'}).body;","handlingStrategy":"validation","validationCode":"function normalizeEncoding(options) {\n  if (options && options.encoding === null) {\n    options.responseType = 'buffer';\n    delete options.encoding;\n  }\n  return options;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use `responseType: 'buffer'` for raw bytes; never `encoding: null`.","When migrating from `request`, map `encoding: null` -> `responseType: 'buffer'`."],"tags":["options","encoding","response-type","migration","config"],"analyzedSha":"e3924aa1e53a6ca3eb93a43618ce532442a89b40","analyzedAt":"2026-08-03T19:22:24.770Z","schemaVersion":2}