Axios Post Json Body

Posted on  by admin

The easiest way to make a POST request with Axios is theaxios.post() function. The first parameter to axios.post() is the URL, and the 2ndis the HTTP request body. By default, if the 2nd parameter to axios.post() is an object, Axiosserializes the object to JSON using the JSON.stringify() function.If the 2nd parameter is an object, Axios also sets the content-type header to application/json, somost web frameworks, like Express, will be ableto automatically convert the request body into a JavaScript object for you.

To override the content-type header in Axios,you should use the third parameter to axios.post(): the options parameter.Set the options.header['content-type'] option to set the content-type header.

POST request using axios with set HTTP headers

If you pass a string as the body parameter to axios.post(), Axios willset the content-type header to application/x-www-form-urlencoded.That means the request body should be a bunch of key/value pairs separated by&, like key1=value1&key2=value2.

You can also POST using JavaScript's FormData class to POST more sophisticated data, including files. Modified5 months ago. Hi I Was wondering how I can send a a single axios post request containing a json object as the request body and also multipart form data (Mp4 file).In my example I want to send 'details' and 'file'.

Installing axios from npm