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

133
Visualizações
Weird, Request failed with status code 400

I am using Axios to send requests to the server. This is the instance I use:

export default axios.create({
  baseURL: "https://wagal.com",
  headers: {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": `Bearer ${store.getState().auth.userData.token}`,
  },
});

and here is how I use above instance to enable and disable 2fa:


function switchTfa(data) {
  if (data) {
    return (dispatch) => {
      return api_request
        .post("/api/user/tfa/enable", {})
        .then((response) => {
          dispatch(isLoading(false));
          dispatch(
            userData({
              tfa_enabled: data,
              user_token: store.getState().auth.userData.token,
            })
          );
          console.log("Success");
        })
        .catch((error) => {
          dispatch(isLoading(false));
          console.log(`error.message`, error.message);
        });
    };
  } else {
    return (dispatch) => {
      dispatch(isLoading(true));
      return api_request
        .post("/web-api/user/tfa/disable", {})
        .then((response) => {
          dispatch(isLoading(false));
          dispatch(
            userData({
              tfa_enabled: data,
              user_token: store.getState().auth.userData.token,
            })
          );
          console.log("Success");
        })
        .catch((error) => {
          dispatch(isLoading(false));
          console.log(`error.message`, error.message);
        });
    };
  }
}

Now, if I send this request, I get below error:

Request failed with status code 400

If I unquote "Authorization" in Axios instance it works

export default axios.create({
  baseURL: "https://wagal.com",
  headers: {
    "Content-Type": "application/json; charset=utf-8",
    Authorization: `Bearer ${store.getState().auth.userData.token}`,
  },
});

Now, if I reload the app and send the request, it throws this 400 error, but if I wrap "Authorization" with quote again, it sends the request, weird!!!

Why after every reloads I get 400 errors sometimes by quoting and sometimes by unquoting "Authorization"?

Thank you

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

0

This is because the store is not persistent. As I can imagine from your code that state is not maintained globally such as Redux, hence when you refresh then the store is empty. Ideally when we want access token to be sent for API call then you will have two options,

  1. Store the access token as encrypted in local storage
  2. Use the browser in-memory storage for a more securing way of access token

You can read about more here : https://auth0.com/docs/security/data-security/token-storage#browser-in-memory-scenarios

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