On the problem of using axios as an interceptor and adding token in the request header

encapsulate a request module with axios. Except for registering and obtaining CAPTCHA, other APIs need to pass token, in headers. Headers can be picked up and written, for example:

// axios.defaults.headers = {
//   "appId": APPID,
//   "token": token,
//   "Content-Type": "application/x-www-form-urlencoded"
// }

but for the two interfaces mentioned above, I have to write those two interfaces separately without token,. Is there anything I can do?

Mar.04,2021

use interceptor

axios.interceptors.request.use(function (config) {
    // 
    if (config.url){
        config.headers['X-Token'] = xtoken  
    }
    return config;
  }, function (error) {
    // 
    return Promise.reject(error);
  });

in fact, don't worry about it. If the interface doesn't need the token, backend, the people on the backend will naturally deal with it accordingly.


create different instances for different situations and use

as needed
const instance1=axios.create(...)
//instance1.defaults.headers={}
const instance2=axios.create(...)
//instance2.defaults.headers={}
export default {
    instance1,
    instance2
}

also write in the interceptor:

const exceptUrls = ['a.xx/xx', 'b.xx/xx']
const requestBefore = (config) => {
    if (exceptUrls.indexOf(config.url)!==-1){
          //  token
    }  
    return config;
}

axios.interceptors.request.use(requestBefore);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bcdd31-30bb8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bcdd31-30bb8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?