Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Axios post request to send form data

Axios POST Request:

var body = {
userName: 'Fred',
userEmail: 'Flintstone@gmail.com'
}

axios({
method: 'post',
url: '/addUser',
data: body
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});


If I set headers as:

headers:{
Content-Type:'multipart/form-data'
}


If I make the related offer in postman it's working fine and sets values to my POJO class.

Can anyone describe how to establish a boundary or how can I send form data using Axios.
by

2 Answers

rahul07
You can post axios data by using FormData() like:

var bodyFormData = new FormData();

And then add the fields to the form you want to send:

bodyFormData.append('userName', 'Fred');

If you are uploading images, you may want to use .append

bodyFormData.append('image', imageFile);

And then you can use axios post method (You can amend it accordingly)

axios({
method: "post",
url: "myurl",
data: bodyFormData,
headers: { "Content-Type": "multipart/form-data" },
})
.then(function (response) {
//handle success
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
kshitijrana14
Check out querystring.
You can use it as follows:
var querystring = require('querystring');
axios.post('url', querystring.stringify({ foo: 'bar' }));

Login / Signup to Answer the Question.