Web - Fetch API

> (World Wide) Web - (W3|WWW) > (HTTP|HTTPS) - Hypertext Transfer Protocol

1 - About

The fetch function is a promise.

Advertising

3 - Syntax

fetch(url, {
    body: JSON.stringify(data), // must match 'Content-Type' header
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *omit
    headers: {
      'user-agent': 'Mozilla/4.0 MDN Example',
      'content-type': 'application/json'
    },
    method: 'POST', // *GET, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *same-origin
    redirect: 'follow', // *manual, error
    referrer: 'no-referrer', // *client
    credentials: 'same-origin'
  })
  .then(response => response.json()) // parses response to JSON

3.1 - Credentials

Credentials are HTTP cookies, TLS client certificates, and authentication entries (for HTTP authentication). For CORS requests, use the “include” value to allow sending credentials to other domains:

3.2 - Mode

  • Using mode: “no-cors” will give you an opaque response, which doesn't seem to return data in the body. See Opaque Response

4 - Management

4.1 - Polyfill

5 - Documentation / Reference

web/http/fetch.txt · Last modified: 2019/02/16 17:40 by gerardnico