Jani Devang
Jani Devang

Reputation: 1109

Attach custom headers before every fetch request in react

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

Answers (1)

Davorin Ruševljan
Davorin Ruševljan

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

Related Questions