Here is how I do my request in ReactJs:
export const uploadNewParticipant = createAsyncThunk(
"projectGroups/uploadNewParticipant",
async ({ userId, projectId, withMail, file }: IProjectNewParticipantsCsv & IAppLoaderAction) => {
const formData = new FormData();
const data = {};
data["userId"] = userId.toString();
data["withMail"] = withMail.toString();
if (projectId) {
data["projectId"] = projectId.toString();
}
formData.append("data", JSON.stringify(data));
formData.append("files", file, file.name);
const response = await axios.post<Array<IProjectGroupElement>>(`${apiUrls.csv.add}`, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
});
return { data: (await response).data };
},
);
And in strapi v4 I have this:
customController.addEndusersCsv = async (ctx) => {
const files = ctx.request.files;
console.log("ctx.request.files: ", ctx.request.files);
console.log("ctx.request.body: ", ctx.request.body);
console.log("ctx.request.body.data: ", ctx.request.body.data);
const data = ctx.request.body.data;
const { userId, projectId, withMail } = data;
console.log("Parsed data:", userId, projectId, withMail);
console.log("Files:", files);
...
}
Here is the logs that strapi gives me:
ctx.request.files: {}
ctx.request.body: { data: '[object FormData]' }
ctx.request.body.data: [object FormData]
Parsed data: undefined undefined undefined
Files: {}
Why ctx.request.body.data = [object FormData]???
This topic has been created from a Discord post (1219667770912804944) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord