global.TextEncoder
Turns strings into UTF-8 bytes, for hashing, uploading or writing them.
Usage
Send text, form fields or bytes with fetch.
// Send text, form fields or bytes with fetch.
async function post(url: string, body: BodyInit) {
const response = await fetch(url, { method: 'POST', body });
if (!response.ok) throw new Error(`${url} answered ${response.status}`);
}
await post('https://api.example.com/login', new URLSearchParams({ user: 'ana', remember: '1' }));
await post('https://api.example.com/notes', 'Plain text, sent as UTF-8');
await post('https://api.example.com/upload', new TextEncoder().encode('bytes'));Constructors
(): TextEncoderParameters
No parameters.
Returns
TextEncoder
Properties and methods 4
| Name | Type | Required | Description |
|---|---|---|---|
prototypereadonly | TextEncoder | Yes | — |
encodingreadonly | "utf-8" | Yes | — |
encode | (text?: string) => Uint8Array | Yes | — |
encodeInto | (text: string, into: Uint8Array) => { read: number; written: number;} | Yes | — |
encode — method signatures
Signatures
(text?: string): Uint8ArrayParameters
| Name | Type | Required | Description |
|---|---|---|---|
text | string | No | — |
Returns
Uint8Array<ArrayBufferLike>
encodeInto — method signatures
Signatures
(text: string, into: Uint8Array): { read: number; written: number; }Parameters
| Name | Type | Required | Description |
|---|---|---|---|
text | string | Yes | — |
into | Uint8Array<ArrayBufferLike> | Yes | — |
Returns
{ read: number; written: number;}
Generated from the public TypeScript declarations: signatures, props and defaults come from the checked source, descriptions from its JSDoc. No defaults are invented.