Response: bytes() method
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:
- The response body is disturbed or locked.
- There was an error decoding the body content (for example, because the
Content-Encoding
header is incorrect).
RangeError
-
There was a problem creating the associated
ArrayBuffer
. For example, if the data size is more thanNumber.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);
Specifications
Specification |
---|
Fetch Standard # dom-body-bytes |
Browser compatibility
BCD tables only load in the browser