-
I have a backend service, it needs an array of strings (multi select input) and an image, for this reason, I sent request using FormData. But when I pushed only one string to the array field, naturally it's sending it as string instead of array of strings. How can I send it as array even it has a only one element? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To send an array within
const formData = new FormData();
const array = ['value1', 'value2', 'value3'];
// Append each item in the array with the same key
array.forEach((item) => {
formData.append('key[]', item);
});
const formData = new FormData();
const array = ['value1', 'value2', 'value3'];
// Append the JSON stringified array to the FormData
formData.append('key', JSON.stringify(array)); These methods provide flexibility depending on your server-side processing needs. |
Beta Was this translation helpful? Give feedback.
To send an array within
FormData
using Axios, you have two primary options depending on how you want to handle the data on the server side:This method is useful when you want the server to interpret the array as individual key-value pairs.
JSON.stringify
:This method is useful when you want to send the entire array as a single entity.