I'm encountering some difficulties in parsing a request sent from the front-end using FormData. Below is an example request generated from Postman for Axios in node.js. Interestingly, when I use the same request in the Postman app, it functions as intended.
This code snippet showcases how to generate a request from Postman feature in frontend:
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();
data.append('file', fs.createReadStream('/some_file.jpg')); **//Electron allows client-side access to FileSystem**
data.append('resizeLargeImage[width]', '1920');
data.append('resizeLargeImage[height]', '1080');
data.append('resizeLargeImage[type]', 'cover');
var config = {
method: 'post',
url: 'localhost:3030/api/v1/optimize-single',
headers: {
'x-api-key': '123',
...data.getHeaders()
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
The backend implementation is as follows:
@Post('/optimize-single')
@UseInterceptors(FileInterceptor('file'))
async uploadFile(
@UploadedFile() file: FileDto,
@Body() body: UploadFileParametersDto,
@Res() response: Response,
): Promise<any> {
console.log('file', file, 'body', body);
**//While "body" is a null Object, "file" turns out to be undefined**
return await this.appService.uploadFile(file, body, response);
}
I'm puzzled as to why Nest isn't able to recognize this type of request. Any insights would be greatly appreciated!
Thank you!