javascript - 即使在 Cypress 中尝试将 pdf 文件作为多部分 FormData POST 请求发送时,FormData 对象仍为空
问题描述
我想将 pdf 文件作为 POST 请求发送。
API 接受@RequestPart
和@RequestParam
:
@RequestPart("file") MultipartFile file;
@RequestParam(value = "document-types", required = false) Set<String> documentTypes;
我试图这样做:
it('test', () => {
cy.fixture(pdfFilePath, "binary").then(file => {
const data = new FormData();
data.set('file', file);
data.set('document-types', 'New Type');
cy.request({
method: "POST",
url: '/api/v4/documents',
headers: {
accepts: "multipart/form-data",
authorization: authString
},
body: data
}).then((response) => {
expect(response.status).to.eq(200)
});
});
});
当我运行它时,我得到:
Status: 400 - Bad Request
在赛普拉斯,我看到发送的请求中的正文是空的:
Body: {}
当我尝试调试代码时,我看到data
发送的是空的,正如您在所附屏幕截图中看到的那样:
我想知道为什么,因为我尝试data
通过在上面屏幕截图的第 154 行和第 155 行中执行这些步骤来设置:
data.set('file', file);
data.set('document-types', 'New Type');
我在这里想念什么?
柏树版本:9.0.0
解决方案
你必须附加formData,下面的代码只是一个例子:
const formData = new FormData()
formData.append('file', files[0])
formData.append('name', files[0].name)
推荐阅读
- javascript - 将具有多个数组的对象转换为对象数组
- node.js - 将 MongoDB ID 存储为我在 Amazon S3 上的密钥
- reactjs - 将 setInterval 与 useState 反应
- c++ - 为什么我们需要在 C++ 字符串中使用空终止符?
- c# - 支持 Internet Explorer 11 上的实时流 RTSP
- list - 共享点列表到 Power Query 加载
- azure-service-fabric - 从 javascript 调用部署在服务结构上的 web api 时出现选择证书弹出窗口
- mysql - MYSQL 错误 1292 Incorrext datetime value while insert into table
- python - 将 python3.7 升级到 python3.8 后 pipenv、jupyter 和可能的其他命令停止工作
- elasticsearch - 如何从弹性搜索中克隆现有索引数据