Miragejs handles json out-of-the-box but seems to be unable to handle FormData. When I post FormData and log the received request
in mirage endpoint, request.requestBody
is empty.
Simplified code examples:
- POSTing FormData:
const testFile = new File(['hello'], 'hello.png', { type: 'image/png' });
const formData = new FormData('file', testFile);
fetch('https://localhost:3000/api/endpoint', {method: 'POST', body: formData});
// ...
- receiving POST in mirage mock server:
this.post('/endpoint', (schema, request) => {
console.log('request:', request);
// request.requestBody is an empty string!
});
Possibly a related issue: https://github.com/miragejs/ember-cli-mirage/issues/74
It's possible to cast the
request.requestBody
toFormData
and then parse the file.Shortening the excellent solution described in How to handle uploading and parsing files in your frontend app tests: