In one of my endpoints I need to add a multipart/form-data. the formData is created like so:
// inputData is just an object: { info: 'something' } created by a form.
// file is an image but irrelevant in this question
const blob = new Blob([inputData], { type: 'application/json' });
const formData = new FormData();
formData.append('data', blob);
formData.append('file', file);
When I intercept this endpoint using MSW and access the body in the request I can see the file
and data
. To access data I'm using the follow code:
const addInfo = rest.post(url, async (req, res, ctx) => {
const { data } = req.body;
const text = await data.text();
const { info } = JSON.parse(text);
return res(ctx.status(200), ctx.json({ info }));
});
When running my Playwright tests, this works fine. data
is found and data.text()
returns the text. But the problem is, when I enter the exact same information as my Playwright tests, await data.text()
returns an empty string. When I look in data
the size of the blob is 0
while the size actually has a number when running from my Playwright tests.
Can someone explain me why it works with my Playwright tests?