Utilisation

Send text, form fields or bytes with fetch.

tsx
// 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'));

Déclaration du type

ts
type BodyInit = string | ArrayBuffer | ArrayBufferView | URLSearchParams;

Membres 1

NomTypeRequisDescription
toString(() => string) | (() => string) | (() => string)OuiReturns a string representation of a string. Returns a string representation of an object. application/x-www-form-urlencoded: spaces as +.
toString — signatures de méthode

Signatures

ts
(): string

Paramètres

Aucun paramètre.

Retourne

string

Généré depuis les déclarations TypeScript publiques : signatures, props et valeurs par défaut viennent de la source vérifiée, les descriptions de sa JSDoc. Aucune valeur par défaut n'est inventée.