close

[Solved] Axios POST request fails Error: Request failed with status code 500 Internal Server error

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Axios POST request fails Error: Request failed with status code 500 Internal Server error in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How To Solve Axios POST request fails Error: Request failed with status code 500 Internal Server Error ?

  1. How To Solve Axios POST request fails Error: Request failed with status code 500 Internal Server Error?

    To Solve Axios POST request fails Error: Request failed with status code 500 Internal Server Error In the catch block, the error will always be 500 internal server error so, use error.response.data instead of an error.

  2. Axios POST request fails Error: Request failed with status code 500 Internal Server error

    To Solve Axios POST request fails Error: Request failed with status code 500 Internal Server Error In the catch block, the error will always be 500 internal server error so, use error.response.data instead of an error.

Solution 1

  • In the catch block, the error which will always be 500 internal server error
  • so, use error.response.data instead of error.
try {
  let result = await axios.post(          // any call like get
    "http://localhost:3001/user",         // your URL
    {                                     // data if post, put
      some: "data",
    }
  );
  console.log(result.response.data);
} catch (error) {
  console.error(error.response.data);     // NOTE - use "error.response.data` (not "error")
}

Solution 2

So I also got stuck in the same problem and the solution that I found was something like this :

let data = JSON.stringify({
  username: this.state.username,
  password: password
});

const response = axios.post(url,data,{headers:{"Content-Type" : "application/json"}});

This solution worked for me.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment