Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers are not able to add #49

Open
RaruRv opened this issue Aug 6, 2020 · 3 comments
Open

Headers are not able to add #49

RaruRv opened this issue Aug 6, 2020 · 3 comments

Comments

@RaruRv
Copy link

RaruRv commented Aug 6, 2020

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

@iSanjayAchar
Copy link

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);
    }
});

@RaruRv
Copy link
Author

RaruRv commented Aug 10, 2020

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);
}

});
`

@vedic-tech
Copy link

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;`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants