How can I create a controller in Nest.js that accepts two form-data inputs?
Here is my Angular service code:
public importSchema(file: File, importConfig: PreviewImportConfig): Observable<HttpEvent<SchemaParseResponse>> {
const formData = new FormData();
formData.append('importConfig', new Blob([JSON.stringify(importConfig)], { type: 'application/json' }));
formData.append('file', file);
const req = new HttpRequest('POST', url, formData, {
reportProgress: true,
responseType: 'json',
});
return this.http.request<SchemaParseResponse>(req);
}
}
And here is the controller code to handle the incoming request:
@Post('/import-schema')
@UseInterceptors(FileInterceptor('file'))
@UseInterceptors(FileInterceptor('importConfig'))
previewFile(@UploadedFile() file: any, @Body() importConfig: PreviewImportConfig) {
console.log("File" + file);
return this.schemaUploadService.createPreviewImportWarnResponse();
}
However, I am receiving a Bad Request error.
This is how the payload appears:
------WebKitFormBoundarywAJpjN6KXywEIYis
Content-Disposition: form-data; name="importConfig"; filename="blob"
Content-Type: application/json
{"preview":true,"defaults":{"include":false},"typeConfigs":{}}
------WebKitFormBoundarywAJpjN6KXywEIYis
Content-Disposition: form-data; name="file"; filename="TD_USA_Import_2_warning.xsd"
Content-Type: application/octet-stream
------WebKitFormBoundarywAJpjN6KXywEIYis--