Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using Axios interceptors to set some data and headers in different requests:

export default ({ $axios }) => {
  $axios.onRequest((req) => {
    if (req.url === 'https://identity.365werk.nl/api/login') {
      console.log('login')
      $axios.defaults.headers['Content-Type'] = 'application/json'
      $axios.defaults.headers.Accept = 'application/json'
    }

    if (req.url === 'https://userservice.365werk.nl/api/v0/auth/social') {
      $axios.defaults.headers['Content-Type'] = 'application/vnd.api+json'
      $axios.defaults.headers.Accept = 'application/vnd.api+json'
    }
  }
}

But the request headers of the response show:

enter image description here

If I add the headers outside the if statement:

export default ({ $axios }) => {
  $axios.defaults.headers['Content-Type'] = 'application/vnd.api+json'
  $axios.defaults.headers.Accept = 'application/vnd.api+json'
  $axios.onRequest((req) => {
    if (req.url === 'https://identity.365werk.nl/api/login') {
      console.log('login')
      $axios.defaults.headers['Content-Type'] = 'application/json'
      $axios.defaults.headers.Accept = 'application/json'
    }

    if (req.url === 'https://userservice.365werk.nl/api/v0/auth/social') {
      console.log('social')
    }
  }
}

enter image description here

But those headers are now also set for the /login url:

enter image description here

Which should just have json headers:

  $axios.defaults.headers['Content-Type'] = 'application/json'
  $axios.defaults.headers.Accept = 'application/json'

Any ideas how to conditionally (based on the url) set the headers of that request?

question from:https://stackoverflow.com/questions/66054292/set-headers-based-on-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...