DEV Community

Lam
Lam

Posted on

Fetch Cheat Sheet

References

const fetch = require('isomorphic-fetch')
Enter fullscreen mode Exit fullscreen mode

See: isomorphic-fetch (npmjs.com)

Catching errors

fetch('/data.json')
  .then(checkStatus)
Enter fullscreen mode Exit fullscreen mode
function checkStatus (res) {
  if (res.status >= 200 && res.status < 300) {
    return res
  } else {
    let err = new Error(res.statusText)
    err.response = res
    throw err
  }
}
Enter fullscreen mode Exit fullscreen mode

Non-2xx responses are still successful requests. Use another function to turn them to errors.

Request options

fetch('/data.json', {
  method: 'post',
  body: new FormData(form), // post body
  body: JSON.stringify(...),

  headers: {
    'Accept': 'application/json'
  },

  credentials: 'same-origin', // send cookies
  credentials: 'include',     // send cookies, even in CORS

})
Enter fullscreen mode Exit fullscreen mode

Response

fetch('/data.json')
.then(res => {
  res.text()       // response body (=> Promise)
  res.json()       // parse via JSON (=> Promise)
  res.status       //=> 200
  res.statusText   //=> 'OK'
  res.redirected   //=> false
  res.ok           //=> true
  res.url          //=> 'http://site.com/data.json'
  res.type         //=> 'basic'
                   //   ('cors' 'default' 'error'
                   //    'opaque' 'opaqueredirect')

  res.headers.get('Content-Type')
})
Enter fullscreen mode Exit fullscreen mode

Fetch

fetch('/data.json')
  .then(response => response.json())
  .then(data => {
    console.log(data)
  })
  .catch(err => ...)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)