Reputation: 1109
How can i add custom global header for all my fetch requests in react application. Is there any function to provide global header for all fetch request in react.
getFoo() {
return fetch(apiUrl + "API/foo").then(
function(response) {
return response.json();
});
}
Want to add global hear for my all fetch requests in react , i am stuck here please help me and thanks in advance.
Upvotes: 6
Views: 3069
Reputation: 4392
In your question there is nothing React specific, since fetch is not part of react.
In another words, you can write your own function that attaches headers of your liking and invokes fetch with it. You can make that function global if you really, really wish to do so. You may even monkey patch fetch with packages like fetch-intercept, but there are not many situations that could justify doing so.
Upvotes: 1