0519

ha·2022년 5월 19일
0

Axios 헤더 설정 방법

axios.post('https://example.com/postSomething', {
 email: varEmail, //varEmail is a variable which holds the email
 password: varPassword
},
{
  headers: {
    Authorization: 'Bearer ' + varToken
  }
})

header 전역 설정

axios.defaults.headers.post['header1'] = 'value' // for POST requests
axios.defaults.headers.common['header1'] = 'value' // for all requests

instance에 기본값으로 설정

let instance = axios.create({
  headers: {
    post: {        // can be common or any other method
      header1: 'value1'
    }
  }
})

//- or after instance has been created
instance.defaults.headers.post['header1'] = 'value'

//- or before a request is made
// using Interceptors
instance.interceptors.request.use(config => {
  config.headers.post['header1'] = 'value';
  return config;
});

Local storage 값 추가 방법

var existing = localStorage.getItem("token");
               existing = JSON.parse(existing);
               existing.access_token = res.data.token;
               window.localStorage.setItem("token", JSON.stringify(existing));
               axios.defaults.headers.common['Authorization'] = "JWT " + res.data.token;

1개의 댓글

comment-user-thumbnail
2022년 6월 13일

댓글작성

답글 달기