【发布时间】:2022-10-09 17:39:40
【问题描述】:
我正在使用 Cypress 6.4.0 和 TypeScript 编写 API 测试,我需要在请求正文中上传一个 pdf 文件。 我的请求代码是:
我的请求正文代码是:
public async createAssetDocTest() {
let url = sharedData.createAsset_url + sharedData.assetA;
let response = await fetch(url
,
{
method: 'POST',
body: await requestbody.createAssetDocBody(),
headers: {
Authorization: sharedData.bearer + " " + adminTokenValue,
Accept: sharedData.accept,
'Content-type': sharedData.docReqContent,
},
}
);
expect(response.status).to.equal(200);
public async createAssetDocBody(): Promise<any> {
const file = sharedData.doc;
cy.fixture(file).then((pdfDoc) => {
Cypress.Blob.binaryStringToBlob(
pdfDoc,
sharedData.contentTypeValue
).then(async (blob: string | Blob) => {
const formData = new FormData();
formData.set(sharedData.document, blob, file);
const body = {
formdata: {
document: {
value: pdfDoc,
options: {
filename: sharedData.document,
contentType: null,
},
},
},
};
return body;
});
});
}
但是,文件没有上传文件,请求失败并出现错误 400。有没有更好的方法在 POST 请求的正文中上传文件? enter image description here
【问题讨论】:
标签: cypress