Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
I am not able to send JWT token in headers in react.js

I am trying to send the JWT token in the headers in the Axios API call but in react.js I am not able to send it. I have tried some ways to send it in the header but on the backend, I am not able to get it. And If send the same token on postman then the backend is able to extract the token from the headers.

 const token = window.localStorage.getItem("token");
console.log("token",token) // token

const apiHeaders = {
          headers: {
            "Access-Control-Allow-Origin": "*",
            "Content-Type": "application/json, text/plain, */*",
            "token": `${token}`,
          },
        };
        const result = await Axios.get(
          `${process.env.REACT_APP_BASE_URL}/api/student/AllProjects`,
          {
            withCredentials: true,
            headers: apiHeaders, 
          }
        );


I have also tried sending token as

 token: `${token}`
or

 token: token

I don't know what I am doing wrong here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think your problem is that you have headers twice:

const apiHeaders = { headers: { ... } }

So apiHeaders is an object with a headers field.

When you use that in the get call, that gets expanded to what is effectively:

const result = await Axios.get(
          `${process.env.REACT_APP_BASE_URL}/api/student/AllProjects`,
          {
            // You probably don't need to do this! withCredentials: true,
            headers: {
               headers: {
                 token: token
               }
            }, 
          }
        );

BTW, it would be normal to pass that as a "bearer token" in the authorization header:

headers: {
    Authorization: `Bearer ${token}`
}

Also, you probably don't need the withCredentials when you post the token in a header.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda