Home > front end >  Different response of the axios get depending on the token
Different response of the axios get depending on the token

Time:11-10

I'm currently implementing the liked function.

so, When entering the component, an Axios get request is made with async created().

In postman, axios get requests from members and axios get requests from non-members depend on the presence or absence of tokens.

if I put the token in the header and make an axios get request, I make an axios get request when I'm a member, and if I make an axios get request without putting the token in the header, I make an axios get request when I'm a non-member.

As above, In postman, the response of the axios get is different depending on whether you put the token in the header or not. But, In my code, Even if I put a token in the header and make an axios get request, the backend log outputs that I have made an axios get request as a non-member.

How can I get the response differently when I make an axios get request depending on the non-member and the member?

getHeart(state, payload) {
  const {id} = payload
  axios.get(`http://??.???.???.???:????/api/books/${id}/heart`, {
    headers: {
      Authorization: `Bearer`   localStorage.getItem('user-token')
    }
  })
  .then(res => {
    console.log(res)
  })
  .catch(err => {
    console.log(err)
  })
}

My axios get code is like this.

CodePudding user response:

It seems that you don't have a space between 'Bearer' and token, try:

Authorization: `Bearer `   localStorage.getItem('user-token')

or even:

Authorization: `Bearer ${localStorage.getItem('user-token')}`
  • Related