Madeline Ries
Madeline Ries

Reputation: 629

Axios get passing header not working if use query param as second argument

I manage to use axios to do a GET request with header

fetchUsers = ({offset = 1, q = ''} = {}) => new Promise(function (resolve, reject) {

  const url = `localhost:3000/users`

  axios.get(url, {
    headers: { token: 123 }
  }).then(response => {

  })
})

But I also want optional query param for pagination and search, I do

fetchUsers = ({offset = 1, q = ''} = {}) => new Promise(function (resolve, reject) {

  const url = `localhost:3000/users`

  axios.get(url, {params: {offset, q}}, {
    headers: { token: 123 }
  }).then(response => {

  })
})

I don't see my token in the header anymore, any clue?

Upvotes: 1

Views: 1768

Answers (1)

Evgeny Kuznetsov
Evgeny Kuznetsov

Reputation: 2188

I think, you should try to move your headers to object with params:

  axios.get(url, {params: {offset, q}, 
                  headers: { token: 123 }}

Upvotes: 3

Related Questions