forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FF128 Request.bytes(), Response.bytes() (mdn#34370)
- Loading branch information
1 parent
478a7e5
commit 9dfdc85
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: "Request: bytes() method" | ||
short-title: bytes() | ||
slug: Web/API/Request/bytes | ||
page-type: web-api-instance-method | ||
browser-compat: api.Request.bytes | ||
--- | ||
|
||
{{APIRef("Fetch API")}} | ||
|
||
The **`bytes()`** method of the {{domxref("Request")}} interface reads the request body and returns it as a promise that resolves with an {{jsxref("Uint8Array")}}. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
bytes() | ||
``` | ||
|
||
### Parameters | ||
|
||
None. | ||
|
||
### Return value | ||
|
||
A promise that resolves with an {{jsxref("Uint8Array")}}. | ||
|
||
## Examples | ||
|
||
```js | ||
const myArray = new Uint8Array(10); | ||
|
||
const request = new Request("/myEndpoint", { | ||
method: "POST", | ||
body: myArray, | ||
}); | ||
|
||
request.bytes().then((buffer) => { | ||
// do something with the buffer sent in the request | ||
}); | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Response.arrayBuffer()")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: "Response: bytes() method" | ||
short-title: bytes() | ||
slug: Web/API/Response/bytes | ||
page-type: web-api-instance-method | ||
browser-compat: api.Response.bytes | ||
--- | ||
|
||
{{APIRef("Fetch API")}} | ||
|
||
The **`bytes()`** method of the {{domxref("Response")}} interface takes a {{domxref("Response")}} stream and reads it to completion. | ||
It returns a promise that resolves with a {{jsxref("Uint8Array")}}. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
bytes() | ||
``` | ||
|
||
### Parameters | ||
|
||
None. | ||
|
||
### Return value | ||
|
||
A promise that resolves with an {{jsxref("Uint8Array")}}. | ||
|
||
### Exceptions | ||
|
||
- `TypeError` | ||
- : The response body has already been read from, or the associated stream is locked or has been cancelled. | ||
- `RangeError` | ||
- : There is a problem creating the associated `ArrayBuffer`. | ||
For example, if the data size is more than [`Number.MAX_SAFE_INTEGER`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/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 {{jsxref("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 | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- [ServiceWorker API](/en-US/docs/Web/API/Service_Worker_API) | ||
- [HTTP access control (CORS)](/en-US/docs/Web/HTTP/CORS) | ||
- [HTTP](/en-US/docs/Web/HTTP) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters