I am trying to make a POST request to an API server and I am sending an array of JSON, the problem is that I get this error:
cannot unmarshal array into Go value of type models.UserRequest
I tried to unmarshal it using a factory and then initializing the objects, but I still get this error, how can I fix this error and make my request? Here is my code:
import fetch from 'node-fetch';
import xlsx from 'xlsx';
const baseUrl = "";
const apiToken = "";
const accountId = "";
const wb = xlsx.readFile('users.xlsx');
const ws = wb.Sheets['users'];
const data = xlsx.utils.sheet_to_json(ws);
// console.log(data)
const options = {
method: "POST",
headers: {
Authorization: `Bearer ${apiToken}`,
"gtmhub-accountid": accountId,
"Content-type": "application/json; charset=UTF-8",
Accept: "application/json, text/plain, */*",
},
body: JSON.stringify(
data
),
};
const createUser = (url, settings) => {
return fetch(`${url}/users`, settings)
.then((response) => response.text())
.then((data) => console.log(data))
.catch((error) => {
console.log(error.message);
});
};
createUser(baseUrl, options);
You should be able, that you send right object structure to you api service. Also this seems like as the problem on the api service side