Skip to content

Latest commit

 

History

History
40 lines (33 loc) · 921 Bytes

javascript-fetch.md

File metadata and controls

40 lines (33 loc) · 921 Bytes
title description created updated color
JavaScript Fetch API
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses.
2018-11-05
2018-11-05

get

fetch('https://jsonplaceholder.typicode.com/todos')
      .then(res => res.json())
      .then(json => console.log(json))

post formdata

const formData = new FormData();
formData.append('name', 'foo');

fetch(`[url]`, { method: 'POST', body: formData})
        .then(res => res.text())
        .then(text => console.log(text))

post application/json

await fetch(`[url]`, {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify([dataObject])
    }).then(res => res.json())
      .then(json => console.log(json))