Ok, so I have an endpoint on my NestJs server that accepts BOTH files AND other data properties. It looks like this:
@Post()
@UseInterceptors(
FileFieldsInterceptor([
{ name: 'settingsFile', maxCount: 1 },
{ name: 'imageFile', maxCount: 1}
])
)
async create(
@Body() createCaptionDto: CreatePostDto,
@UploadedFiles()
files: {
settingsFile: Express.Multer.File[];
imageFile: Express.Multer.File[];
}
) {...
CreatePostDto
looks like this:
export class CreatePostDto {
...
@IsArray()
@ArrayMinSize(1)
// @ValidateNested({ each: true })
// @Type(() => PostSource)
sources: Array<PostSource>;
@IsArray()
@IsString({ each: true })
tags: Array<string>;
}
The problem arises when I try to call this endpoint from the front-end. I am using JS fetch API, and to send the multiple files, I am using JS FormData object as the fetch API body, as per the NestJs documentation and this question.
this works well for almost all values I wish to put in the body. As you can see from the CreatePostDto above, the tags array of strings works perfectly. I construct it like this:
for (const tag of createCaptionDto.tags) {
formData.append('tags[]', tag);
}
Here every tag is a string. But when I need an array of objects like in the Sources array, I run into many different problems.
If I try...
formData.append('sources[]', JSON.stringify(sources));
, the server parses it as:
['[{attr1: 223, attr2: 3737}, {attr1: 235325, attr2: 5366}]']
So it is just the stringified array of objects inside another array. This is not right.
So then I try...
for (const source of createCaptionDto.sources) {
formData.append('sources[]', JSON.stringify(source));
}
, the server parses it as:
['{attr1: 223, attr2: 3737}', '{attr1: 235325, attr2: 5366}']
Closer, but still not right.
So then I try...
formData.append('sources', JSON.stringify(sources));
... but NestJs doesn't think this is an array.
So it seems no matter what I do, there is a fundamental mismatch in what serialization needs to be done to send the request through FormData and what deserialization is being done on the server. Does anyone have any idea what I can do about this? Or have done something similar before? Thank you