curl → fetch
Van een curl-commando fetch-code maken.
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.
{"customer":"ada@example.com","lines":[{"sku":"A-1","quantity":2}]}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();