Response: bytes() method

Baseline 2025
Newly available

Since January 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Note: This feature is available in Web Workers.

The bytes() method of the Response interface takes a Response stream and reads it to completion. It returns a promise that resolves with a Uint8Array.

Syntax

js
bytes()

Parameters

None.

Return value

A promise that resolves with an Uint8Array.

Exceptions

DOMException AbortError

The request was aborted.

TypeError

Thrown for one of the following reasons:

RangeError

There was a problem creating the associated ArrayBuffer. For example, if the data size is more than Number.MAX_SAFE_INTEGER.

Examples

Fetching and decoding a file

The code below shows how you might fetch a text file, return the body as a Uint8Array, and then decode this into a string.

js
const response = await fetch("https://www.example.com/textfile.txt");
const textFile = await response.bytes();
const string = new TextDecoder().decode(textFile);
console.log(string);

Getting an image file signature

This example demonstrates how you can use bytes() to read the signature bytes of a number of image files, and identify the type.

HTML

First we define a

JavaScript

The code first checks if the bytes() method is supported. If the method is supported it adds an event handler for the change event on the