{"id":726,"hash":"0596aff013f814ad90bc110dab4c0a9e67c6614bc90abe8daf232fb88c46c959","pattern":"catching error body using axios post","full_message":"I am sending a status code 422 from my backend code with response body which contains the description of the error. I am using axios post as below to post a request:\n\npost: function(url, reqBody) {\n    const request = axios({\n        baseURL: config.apiUrl,\n        url: url,\n        headers: {\n            'Content-Type': 'application/json',\n            'Authorization': sessionStorage.getItem('token')\n        },\n        method: 'POST',\n        data: reqBody,\n        responseType: 'json'\n    });\n    return request\n        .then((res) => {\n            return res;\n        })\n        .catch((error) => {\n            console.log(error);\n            return error;\n        })\n}\n\nThe problem is when backend is returning error code 422, the error object I am catching has no information about response body. Is there any way I can retrieve the error text?","ecosystem":"npm","package_name":"post","package_version":null,"solution":"I had this same issue and the answer (as per Axios >= 0.13) is to specifically check error.response.data:\n\naxios({\n    ...\n}).then((response) => {\n    ....\n}).catch((error) => {\n    if( error.response ){\n        console.log(error.response.data); // => the response payload \n    }\n});\n\nSee here for more details.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/45017822/catching-error-body-using-axios-post","votes":125,"created_at":"2026-04-19T04:51:33.559968+00:00","updated_at":"2026-04-19T04:51:33.559968+00:00"}