When I use dev tool like Postman to upload file, my Express.js server using multer
import multer from 'multer';
const upload = multer();
app.post('/api/upload-file', upload.single('file'), (req, res) => {
console.log(req.file);
res.sendStatus(200);
});
can parse the file correctly including mimetype
field and prints
{
fieldname: 'file',
originalname: 'my.png',
encoding: '7bit',
mimetype: 'image/png',
buffer: <Buffer ...>,
size: 9693
}
However, when I use locust (for load testing, and it is requests backed) to upload same file by both of these two methods:
from requests_toolbelt.multipart.encoder import MultipartEncoder
@task
def upload_img_1(self):
file = open('path/to/my.png', 'rb')
self.client.post(
'/api/upload-file',
files={'file': file})
@task
def upload_img_2(self):
file = open('path/to/my.png', 'rb')
m = MultipartEncoder(
fields={'file': ('my.png', file, 'image/png')})
self.client.post(
'/api/upload-file',
files={'file': m})
The mimetype
I got in my Express.js server are text/plain
which is wrong.
{
fieldname: 'file',
originalname: 'my.png',
encoding: '7bit',
mimetype: 'text/plain',
buffer: <Buffer ...>,
size: 9693
}
How to get correct mimetype
?