Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

66
Vistas
Get value from axios response header

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?

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try destructuring it:

.then({data}) => {
    console.log(data.parameter1);
}
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos