JavaScript Fetching External Data

Fetch Request

We can create new Request objects directly and later on pass them to fetch, but this is rarely done. We instead often create Request objects as part of a fetch call.

The second parameter to fetch customizes the Request object. We can set the method of the network request, the headers, the destination URL, whether to include credentials along with a couple of other less used properties (see the MDN link below for full details).

Read more on MDN

Request

Code example: fetch options

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