Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!