I'm calling an API which returns some data and aditional information. This information is located in a custom header. For that I have the following code:
const CallAPI = async param => {
await axios.get("api url...")
.then(response => {
var headerValues = response.headers['x-customName'];
/*This prints something like this:
{"Parameter1":633,"Parameter2":10}*/
console.log(headerValues);
});
}
What I'm trying to do is to get the value of any of those parameters. I tried doing something like this:
console.log(headerValues["Parameter1"]);
console.log(headerValues.Parameter1);
But with both I'm getting undefined result. I also tried converting my response header result to JSON and trying the above console.log using the following code, but I'm getting the same results.
JSON.stringify(response.headers['x-customName']);
What I'm doing wrong or what can I do to get those values?
Try destructuring it:
.then({data}) => {
console.log(data.parameter1);
}