cd .. (alle tools)

curl → fetch

Van een curl-commando fetch-code maken.

// Draait in je browser. Wat je hier typt wordt nergens heen gestuurd.

Een POST-verzoek met 2 headers.

Zoals gekopieerd uit een terminal, documentatie of het Network-tabblad van een browser ("Copy as cURL"), voor bash of voor de Windows-opdrachtprompt.

Het verzoek

methode
POST
adres
https://api.example.com/v1/orders?expand=lines

Headers

Authorization
Bearer eyJhbGciOiJIUzI1NiJ9.e30.abc
Content-Type
application/json

Body

{"customer":"ada@example.com","lines":[{"sku":"A-1","quantity":2}]}
Lees het antwoord als

fetch

const response = await fetch('https://api.example.com/v1/orders?expand=lines', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer eyJhbGciOiJIUzI1NiJ9.e30.abc',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    customer: 'ada@example.com',
    lines: [
      {
        sku: 'A-1',
        quantity: 2
      }
    ]
  })
});
const data = await response.json();