How can i set the token created from the 1st request & use in the next? Tried setting both as "X-Auth-Token": clientToken
or Authorization: Bearer ${clientToken}
, still it doesn't work.
Getting an error stating:
data: {
description: "Missing request header 'X-Auth-Token' for method parameter of type String"
}
This is the request
axios
.post(tokenUrl, credentials, {
headers: { "content-type": "application/json" }
})
.then(function(response) {
const clientToken = response.data;
console.log(clientToken);
console.log("fetched token via api");
//Get Receipt data
const receiptUrl =
"http://rest/financial/receipt";
const dateRange = {
params: {
from: "2020-03-12",
to: "2020-03-13"
}
};
const header = {
headers: {
"X-Auth-Token": clientToken,
"content-type": "application/json"
}
};
axios
.get(receiptUrl, dateRange, header)
.then(function(response) {
console.log(response.data);
console.log("fetched receipts via api");
})
.catch(function(error) {
console.log(error);
});
})
.catch(function(error) {
console.log(error);
});
How can i set the token created from the 1st request & use in the next? Tried setting both as "X-Auth-Token": clientToken
or Authorization: Bearer ${clientToken}
, still it doesn't work.
Getting an error stating:
data: {
description: "Missing request header 'X-Auth-Token' for method parameter of type String"
}
This is the request
axios
.post(tokenUrl, credentials, {
headers: { "content-type": "application/json" }
})
.then(function(response) {
const clientToken = response.data;
console.log(clientToken);
console.log("fetched token via api");
//Get Receipt data
const receiptUrl =
"http://rest/financial/receipt";
const dateRange = {
params: {
from: "2020-03-12",
to: "2020-03-13"
}
};
const header = {
headers: {
"X-Auth-Token": clientToken,
"content-type": "application/json"
}
};
axios
.get(receiptUrl, dateRange, header)
.then(function(response) {
console.log(response.data);
console.log("fetched receipts via api");
})
.catch(function(error) {
console.log(error);
});
})
.catch(function(error) {
console.log(error);
});
Share
Improve this question
asked Mar 13, 2020 at 9:48
mark5mark5
5631 gold badge9 silver badges26 bronze badges
1
- Pass token in headers as bearer token: {'Authorization': 'Bearer ' + access_token} – ainasma Commented Mar 13, 2020 at 10:14
3 Answers
Reset to default 4Try Setting the token in the default headers, if the token exists
if (clientToken) {
axios.defaults.headers.mon["x-auth-token"] = clientToken;
} else {
delete axios.defaults.headers.mon["x-auth-token"];
}
const header = {
headers: {
"content-type": "application/json"
}
};
You may try something like this:
// Other code...
let data = {
params: {
from: "2020-03-12",
to: "2020-03-13"
},
headers: {
"X-Auth-Token": clientToken,
"content-type": "application/json"
}
};
axios.get(receiptUrl, data).then(function(response) {
// ...
}).catch(function(error) {
//...
});
Check the get method signature, which is:
axios.get(URL, { params:{}, headers: { key: 'value' } })
You can set clientToken
globally:
const JWT_TOKEN = 'jwt_access_token';
const api = axios.create({
baseURL: apiURL,
timeout: 5 * 60 * 1000,
});
if (localStorage.getItem(JWT_TOKEN)) {
const token = localStorage.getItem(JWT_TOKEN);
api.defaults.headers.mon.Authorization = `Bearer ${clientToken}`;
}