Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
How can I connect to a API using fetch and bearer token?

I would like to connect to REST API that uses a token Bearer. I try the JS code below :

sessionStorage.setItem('MCToken',
    JSON.stringify('123456')
);

let datastructuresAPI = () => {
    let token = JSON.parse(sessionStorage.getItem('MCToken'));
    let header = new Headers({
        'Cache-Control': 'no-cache',
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
    });
    let response = fetch('https://mon_url',{
        method: 'POST',
        headers: header,
        body:JSON.stringify({
            "methodName":"connect",
            'grant_type': 'client_credentials',
            "serviceName":"ConnectionService"
        }),
        'x-frame': 'same-origin',
        mode: 'no-cors'       
    });
    console.log(response)
}
datastructuresAPI();

But I can’t connect, I have these message (the first is the result of the console.log)

enter image description here

enter image description here

Thank you for your help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Fetch function is an async function and you should wait it.

fetch(...)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.log(error));

For the other result (401) are you sure that your REST API waits token with Bearer {token}? It could be only {token}.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda