JavaScript Fetching External Data

Fetch Response

We get a Response object back when a fetch network request settles. This response object can tell us whether the response was successfull (ok property), the status code of the response (status property) along with a couple of other properties. We can also convert the body of the response, which is a Response stream, to JSON using the json method. The json method returns another Promise that we have to wait for to resolve before we can access the JSON in our code.

Read more on MDN

Response

Code example

fetch("/cars/23", {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json"
}
body: JSON.stringify({
key1: value1,
key2: value2,
})
})
.then(response => response.json())
.then(console.log)