Sending the bearer token with axios

前端 未结 9 1356
野趣味
野趣味 2020-11-28 21:21

In my react app i am using axios to perform the REST api requests.

But it\'s unable to send the Authorization header with the request.

Here is my co

9条回答
  •  难免孤独
    2020-11-28 21:56

    By using Axios interceptor:

    const service = axios.create({
      timeout: 20000 // request timeout
    });
    
    // request interceptor
    
    service.interceptors.request.use(
      config => {
        // Do something before request is sent
    
        config.headers["Authorization"] = "bearer " + getToken();
        return config;
      },
      error => {
        Promise.reject(error);
      }
    );
    

提交回复
热议问题