I went to MDN and read about the fetch API and it says:
You can also optionally pass in an init options object as the second argument
Suppose, we have this simple login function:
const login = () => {
const requestOptions = {
method: "POST",
headers: { "Content-type": "application/json" },
body: JSON.stringify({ username, password })
}
return fetch(`apiUrl/users/authenticate`, requestOptions)
.then(res = res.json)
.then(data => console.log(data))
}
So, requestOptions is an init object here?
Init object is the options with which you can initialize the fetch methods.
The below are the most commonly used options that you can pass to fetch as in init object.
1. method: 'POST', // *GET, POST, PUT, DELETE, etc.
2. mode: 'cors', // no-cors, *cors, same-origin
3. cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
4. credentials: 'same-origin', // include, *same-origin, omit
5. headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
7. redirect: 'follow', // manual, *follow, error
8. referrerPolicy: 'no-referrer', // no-referrer, *client
9. body: JSON.stringify(data) // body data type must match "Content-Type" header
You can read more bout this on MDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With