Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios post array data

Tags:

I'm trying to send post request to a server I don't have much control on it. The only thing I know is I can obtain the correct response if I post the following data in Postman

x-www-form-urlencoded radio button checked  Entered the following 2 array data:     product_id_list[]          pid1234     product_id_list[]          pid1235  Header - Content-Type: application/x-www-form-urlencoded  Method: Post 

But when I tried to do it through axios, it doesn't seems the correct params data can get through. I've tried

axios.post('https://test.com/api/get_product,     querystring.stringify({       'product_id_list': ['pid1234', 'pid1235']     })) . . . axios.post('https://test.com/api/get_product,     querystring.stringify({       'product_id_list[]': 'pid1234',       'product_id_list[]': 'pid1235'     })) . . . 

Anyone got an idea on how to translate this type of array data in axios?

like image 913
Rick Lee Avatar asked Jul 13 '17 05:07

Rick Lee


People also ask

How do I post data on Axios?

To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.

What does Axios post return?

The Axios response object consists of: data - the payload returned from the server. status - the HTTP code returned from the server. statusText - the HTTP status message returned by the server. headers - headers sent by server.

Is Axios Post asynchronous?

Axios is a promise-based HTTP client that lets you handle asynchronous HTTP requests.


1 Answers

This issue popped up for me as well, and I solved it with new FormData().

Having an array:

const product_id_list = ['pid1234', 'pid1235']  const bodyFormData = new FormData();  product_id_list.forEach((item) => {     bodyFormData.append('product_id_list[]', item); });  axios.post('https://test.com/api/get_product', bodyFormData)  

Doing it this way sent it the request as application/x-www-form-urlencoded, and the proper data in the body.

like image 138
David Avatar answered Sep 20 '22 12:09

David