send axios headers instance unique for each request?
up vote
0
down vote
favorite
Is it possible to send axios headers after the instance was created as param (not defaults)?
Let's say I have basic instance via .create()
export const api = axios.create({
baseURL: BACKEND_URL,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
});
I would like to send JWT token which gets updated frequently. There is a routing for 401 error (asking to update token) going to the same instance. When I do token refresh, Authorization header is still appended send and backend through another 401 causing infinite loop.
Most of the answers on StackOverflow leading to axiosIntance.defaults.header
update, but this solution doesn't work for me
// I don't want defaults, just specific for this particular request
// api.defaults.headers['Authorization'] = 'JWT ' + this.props.onboardingToken
// can i send header in params instead ? It doesnt work with this syntax taken from officials docs
const params = {
headers: {
'Authorization': 'JWT ' + this.props.onboardingToken
},
data: {
name: name
}
}
api
.patch(`company/${companyId}`, params)
.then(res)
.catch(err => {
if (err.response.status === 401){
this.refreshToken();
}
})
So, this error-handling request is also with attached headers
refreshToken = () => {
const token = this.props.urlToken
onboardingApi
.post('login-token', {"token": token})
.then(res => {
this.repeatPatch()
})
.catch(err => {
})
}
javascript axios
add a comment |
up vote
0
down vote
favorite
Is it possible to send axios headers after the instance was created as param (not defaults)?
Let's say I have basic instance via .create()
export const api = axios.create({
baseURL: BACKEND_URL,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
});
I would like to send JWT token which gets updated frequently. There is a routing for 401 error (asking to update token) going to the same instance. When I do token refresh, Authorization header is still appended send and backend through another 401 causing infinite loop.
Most of the answers on StackOverflow leading to axiosIntance.defaults.header
update, but this solution doesn't work for me
// I don't want defaults, just specific for this particular request
// api.defaults.headers['Authorization'] = 'JWT ' + this.props.onboardingToken
// can i send header in params instead ? It doesnt work with this syntax taken from officials docs
const params = {
headers: {
'Authorization': 'JWT ' + this.props.onboardingToken
},
data: {
name: name
}
}
api
.patch(`company/${companyId}`, params)
.then(res)
.catch(err => {
if (err.response.status === 401){
this.refreshToken();
}
})
So, this error-handling request is also with attached headers
refreshToken = () => {
const token = this.props.urlToken
onboardingApi
.post('login-token', {"token": token})
.then(res => {
this.repeatPatch()
})
.catch(err => {
})
}
javascript axios
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is it possible to send axios headers after the instance was created as param (not defaults)?
Let's say I have basic instance via .create()
export const api = axios.create({
baseURL: BACKEND_URL,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
});
I would like to send JWT token which gets updated frequently. There is a routing for 401 error (asking to update token) going to the same instance. When I do token refresh, Authorization header is still appended send and backend through another 401 causing infinite loop.
Most of the answers on StackOverflow leading to axiosIntance.defaults.header
update, but this solution doesn't work for me
// I don't want defaults, just specific for this particular request
// api.defaults.headers['Authorization'] = 'JWT ' + this.props.onboardingToken
// can i send header in params instead ? It doesnt work with this syntax taken from officials docs
const params = {
headers: {
'Authorization': 'JWT ' + this.props.onboardingToken
},
data: {
name: name
}
}
api
.patch(`company/${companyId}`, params)
.then(res)
.catch(err => {
if (err.response.status === 401){
this.refreshToken();
}
})
So, this error-handling request is also with attached headers
refreshToken = () => {
const token = this.props.urlToken
onboardingApi
.post('login-token', {"token": token})
.then(res => {
this.repeatPatch()
})
.catch(err => {
})
}
javascript axios
Is it possible to send axios headers after the instance was created as param (not defaults)?
Let's say I have basic instance via .create()
export const api = axios.create({
baseURL: BACKEND_URL,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
});
I would like to send JWT token which gets updated frequently. There is a routing for 401 error (asking to update token) going to the same instance. When I do token refresh, Authorization header is still appended send and backend through another 401 causing infinite loop.
Most of the answers on StackOverflow leading to axiosIntance.defaults.header
update, but this solution doesn't work for me
// I don't want defaults, just specific for this particular request
// api.defaults.headers['Authorization'] = 'JWT ' + this.props.onboardingToken
// can i send header in params instead ? It doesnt work with this syntax taken from officials docs
const params = {
headers: {
'Authorization': 'JWT ' + this.props.onboardingToken
},
data: {
name: name
}
}
api
.patch(`company/${companyId}`, params)
.then(res)
.catch(err => {
if (err.response.status === 401){
this.refreshToken();
}
})
So, this error-handling request is also with attached headers
refreshToken = () => {
const token = this.props.urlToken
onboardingApi
.post('login-token', {"token": token})
.then(res => {
this.repeatPatch()
})
.catch(err => {
})
}
javascript axios
javascript axios
edited Nov 8 at 18:54
asked Nov 8 at 18:46
Sergey Khmelevskoy
1,02211028
1,02211028
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214249%2fsend-axios-headers-instance-unique-for-each-request%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown