JavaScript Fetch API Cheatsheet




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


┬ęCopyright 2020 OneCompiler | Privacy Policy | Terms & Conditions