如何在 Miragejs 中处理 FormData?
Miragejs 可以开箱即用地处理 json,但似乎无法处理 FormData。当我发布 FormData 并在 Mirage 端点中记录收到的请求时,request.requestBody 为空。
简化的代码示例:
- 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});
// ...
- 在 Mirage 模拟服务器中接收 POST:
this.post('/endpoint', (schema, request) => {
console.log('request:', request);
// request.requestBody is an empty string!
});
可能是相关问题:https://github.com/miragejs/ember-cli-mirage/issues/74
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以将
request.requestBody
转换为FormData
,然后解析该文件。缩短 如何在前端应用测试中处理上传和解析文件:
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: