【问题标题】:Unable to upload pdf file in POST request using Cypress 6.4.0 & TypeScript无法使用 Cypress 6.4.0 和 TypeScript 在 POST 请求中上传 pdf 文件
【发布时间】: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


    【解决方案1】:

    解决了这个问题!

    主要问题是我的赛普拉斯版本太旧了。升级到 9.7.0

    然后添加以下代码:

    public async createAssetDoc(): Promise<any> {
        cy.fixture("pic location", "binary")
            .then((file) => Cypress.Blob.binaryStringToBlob(file))
            .then((blob) => {
                var formdata = new FormData();
                formdata.append("document", blob, "pic location");
                const url = "your url";
    
                cy.request({
                    url,
                    method: "POST",
                    body: formdata,
                    headers: {
                        Authorization:
                            "bearer" + " " + token,
                        Accept: "application/json",
                        "Content-Type": "multipart/form-data",
                    },
                }).then((response) => {
                    expect(response.status).to.equal(201);
                    expect(response.statusText).to.equal(
                        sharedData.fileUploadStatusText
                    );
                    return response;
                });
            });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 2014-10-10
      • 2018-06-08
      • 2018-12-16
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多