Hi I'm having issues sending an array with URLSearchParams
. My code is as follows:
const worker = async(endpoint, method, parameters) => {
let body;
if (typeof parameters === 'object' && parameters !== null) {
body = new URLSearchParams()
for (const key in parameters) {
body.append(key, parameters[key])
}
}
try {
const response = await fetch(endpoint, {
method: method,
body: body || null
});
const json = await response.json();
if (json.status === 200) {
return Promise.resolve(json.data)
}
return Promise.reject(json.message);
} catch(error) {
return Promise.reject('500 Encountered a server error')
}
};
I iterate the parameters object and create a new URLSearchParams
object. One of my parameters is an array I have logged the value of the parameter just before body.append(key, parameters[key])
is executed and it is indeed an array: Array(1) ["user"]
. However when I check my express server and read the response the value of the parameter is "user"
and not ["user"]
. I have confirmed it isn't an issue with my express server because the same request works in Postman. What am I doing wrong here?