Verwendung

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

Typdeklaration

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

Mitglieder 1

NameTypErforderlichBeschreibung
toString(() => string) | (() => string) | (() => string)JaReturns a string representation of a string. Returns a string representation of an object. application/x-www-form-urlencoded: spaces as +.
toString — Methodensignaturen

Signaturen

ts
(): string

Parameter

Keine Parameter.

Rückgabe

string

Generiert aus den öffentlichen TypeScript-Deklarationen: Signaturen, Props und Standardwerte stammen aus der geprüften Quelle, Beschreibungen aus deren JSDoc. Kein Standardwert ist erfunden.