fetch-intercept icon indicating copy to clipboard operation
fetch-intercept copied to clipboard

Headers are not able to add

Open RaruRv opened this issue 4 years ago • 4 comments

This might be an issue with the lack of documentation. I am not able to add headers.

RaruRv avatar Aug 06 '20 06:08 RaruRv

Yes, there is a lack of documentation. Below solution is working for me

import fetchIntercept from 'fetch-intercept';
import { env } from './../const';
import AsyncStorage from '@react-native-community/async-storage';

export const unregister = fetchIntercept.register({
    request: async (url, config) => {
        const token = await AsyncStorage.getItem('auth_token');
        const headers = {
            'Content-Type': 'application/json',
            'Authorization': `Token ${ token }`,
            'Accept': 'application/json'
        }

        return [`${env.endpoint}${url}`, { headers, ...config }];
    },
    response: (response) => {
        return response;
    },
    responseError: (error) => {
        return Promise.reject(error);
    }
});

iSanjayAchar avatar Aug 09 '20 19:08 iSanjayAchar

Thankyou for your response, I had solved the issue like :

` import fetchIntercept from 'fetch-intercept'; import { env } from './../const'; import AsyncStorage from '@react-native-community/async-storage';

export const unregister = fetchIntercept.register({ request: async (url, config) => { const token = await AsyncStorage.getItem('auth_token'); const headers = { config.headers = {Authorization: token} }

    return [`${env.endpoint}${url}`, { headers, ...config }];
},
response: (response) => {
    return response;
},
responseError: (error) => {
    return Promise.reject(error);
}

}); `

RaruRv avatar Aug 10 '20 03:08 RaruRv

I solved the issue like below in my RN App,

`import fetchIntercept from 'fetch-intercept';

const fetchInterceptRegister = fetchIntercept.register({ request: function(url, config) { const modifiedHeaders = new Headers(config.headers); modifiedHeaders.append('header_key_name', 'header_value'); modifiedHeaders.append('header_key_name', "header_value"); config.headers = modifiedHeaders;

return [url, config];

},

requestError: function(error) { // Called when an error occured during another 'request' interceptor call return Promise.reject(error); },

response: function(response) { // Modify the reponse object return response; },

responseError: function(error) { // Handle an fetch error return Promise.reject(error); }, });

export default fetchInterceptRegister;`

vedic-tech avatar Oct 08 '20 17:10 vedic-tech