How do I add headers for requests from an API?
Tutorial
by Er Satya
How do I add headers for requests from an API?
// Example POST method implementation:
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return await response.json(); // parses JSON response into native JavaScript objects
}
postData('https://example.com/answer', { answer:
08 })
.then((data) => {
console.log(data); // JSON data parsed by `response.json()` call
});
fetch(url, {
headers: {
"User-Agent": "MyAppName/Version# (Language=Javascript; Platform: Linux)"
}
}).then(function(resp) {
return resp.json();
}).then(function(data){...
All review comments
More articles from same author:
More articles:
Recent lost & found:
Login for enhanced experience
Create and manage your profile
Refer an author and get bonus Learn more
Publish any lost and found belongings
Connect with the authors & add your review comments
Join us for Free to advertise for your business or
Contact-us for more details
Join us for Free to publish your own blogs, articles or tutorials and get your
Benefits
Trending authors
Discover your area of interest
Advertisement
Art & entertainment
Astrology & spirituality
Cooking
Culture
Current affairs
Education
Fashion
History
Hotel management
Industry
Medical & fitness
Motivational
Politics
Real life stories
Sports
Story & poetry
Technology
Top in search
Tourism
More recent categories
Buy Oak Charcoal (BBQ Grade) O(Public)
By: swedpolllc
Buy Premium Sawdust (Loose & D(Public)
By: swedpolllc
Buy Sawdust (Loose and Dry) in(Public)
By: swedpolllc
Buy Kiln-Dried Oak Firewood in(Public)
By: swedpolllc
Buy Premium Loose & Dry Sawdus(Public)
By: swedpolllc
Where to Buy High-Quality Loos(Public)
By: swedpolllc
Buy Sawdust in Germany ? Premi(Public)
By: swedpolllc
Buy Sawdust Online ? Premium Q(Public)
By: swedpolllc
Buy Wood Chips (LOOSE) in Latv(Public)
By: swedpolllc
Buy Loose Wood Chips in German(Public)
By: swedpolllc