Attach Authorization header for all axios requests

前端 未结 9 1979
灰色年华
灰色年华 2020-12-04 05:17

I have a react/redux application that fetches a token from an api server. After the user authenticates I\'d like to make all axios requests have that token as an Authorizati

9条回答
  •  失恋的感觉
    2020-12-04 05:30

    Try to make new instance like i did below

    var common_axios = axios.create({
        baseURL: 'https://sample.com'
    });
    
    // Set default headers to common_axios ( as Instance )
    common_axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
    // Check your Header
    console.log(common_axios.defaults.headers);
    

    How to Use it

    common_axios.get(url).......
    common_axios.post(url).......
    

提交回复
热议问题