Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios post request to send form data

axios POST request is hitting the url on the controller but setting null values to my POJO class, when I go through developer tools in chrome, the payload contains data. What am I doing wrong?

Axios POST Request:

var body = {     userName: 'Fred',     userEmail: '[email protected]' }  axios({     method: 'post',     url: '/addUser',     data: body }) .then(function (response) {     console.log(response); }) .catch(function (error) {     console.log(error); }); 

Browser Response:

enter image description here

If I set headers as:

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

The request throws the error

Error in posting multipart/form-data. Content-Type header is missing boundary

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

Can anyone explain how to set boundary or how can I send form data using axios.

like image 510
Srikanth Gowda Avatar asked Dec 04 '17 09:12

Srikanth Gowda


People also ask

How do you send FormData in Axios POST request in React?

To Use Axios POST Request to Send Form Data in ReactJS First Of all, make a variable named bodyFormData with FormData(). Then You can simply append your form data in bodyFormData just like this: bodyFormData. append('userName', 'milan'); and then you can simply use this bodyFormData in your axios post request data.

How send FormData in Axios put request?

Sending a PUT Request with Axios The simplest way to make the PUT call is to simply use the put() function of the axios instance, and supply the body of that request in the form of a JavaScript object: const res = await axios. put('/api/article/123', { title: 'Making PUT Requests with Axios', status: 'published' });

How can I send FormData in POST request?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

Can we send data in Axios get request?

Axios can make a GET request to “get” data from a server API. The axios. get() method is used to make an HTTP get request. There are two parameters that must be passed to the Axios get() method.


1 Answers

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);   }); 

Related GitHub issue:

Can't get a .post with 'Content-Type': 'multipart/form-data' to work @ axios/axios

like image 182
Aaqib Avatar answered Sep 27 '22 21:09

Aaqib