Axios Post Response

Posted on  by admin

When you await on an Axios request, you get back an Axios response. An Axios response is a POJO with severalproperties, including data, which contains the parsed response body.

An Axios response contains several other properties, like status, which contains the HTTP response statuscode (like 200 or 404). But most of the time you don't care about the response code if the requestsucceeded, so you will often see code that gets the response body directly using promise chaining. You can also get the response body using destructuring assignments.

Axios parses the response based on the HTTP response's Content-Type header. When the response's content typeis application/json, Axios will automatically try to parse the response into a JavaScript object. Keep in mind that the response headers are sent by the server.

Simple POST request with a JSON body using axios

So if the server sends back a different contenttype, you may need to handle it the response yourself. For other content types, like text/html, the property will be a string. You can configure the type of the data property using Axios' responseType object. By default,responseType is set to 'json', which means Axios will try to parse the response as JSON.

However, that isn't correct if you're looking to, say, download an image using Axios. You can set responseType to 'arraybuffer' to get the response as an ArrayBuffer:.