Getting error handler data from post request in AXIOS

Viewed 2407

Can you tell me how can I consume a response from a post type request, in axios, which gives an error 422 (Unprocessable Entity)? I can't consume the JSON response from API, which would be, precisely, the error handling, with some information I need: example - { error: "ERROR_FROM_X_TO_Y" } (I'll need error attribute).

const postReq = () => {
  return async () => {
    try {
      const url = '/exampleUrl/account-data';
      try {
        const response = await axios.post(url);
      } catch (err) {
        console.log(err);
      }
    } catch (error) {
      console.log("error");
    }
  };
};

I already tried to console.log the response, but it doesn't works.

1 Answers

Actually the error is stocked in your response const, did you tried :

await axios.post(url).catch((err) => { console.error(err) });

Edit : I found out this issue on github, i hope she can be revelant for solve your problem :

axios.post('/formulas/create', {
            name: "",
            parts: ""
}).then(response => { 
      console.log(response)
    }).catch(error => {
      console.log(error.response)
    });
   

https://github.com/axios/axios/issues/960

Related