在 Postman 中使用 multipart/form-data 命中方法 2 次上传文件的预请求脚本 [英] Pre request script of upload file with multipart/form-data hitting method 2 times in Postman

查看:21
本文介绍了在 Postman 中使用 multipart/form-data 命中方法 2 次上传文件的预请求脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 API 项目并使用 Postman 编写测试用例,以便自动化检查 API 状态.这里我有一种上传方法,用户必须将文件上传到服务器并需要检查服务器是否返回适当的响应.

I'm working with API project and writing test cases with Postman for automation to check API status. Here I have one upload method in which user has to upload a file to the server and need to check if the server returns an appropriate response.

从邮递员那里接受带有多部分/表单数据的请求的上传方法,我传递如下屏幕:

Upload method accepting the request with multipart/form-data, from Postman I'm passing as below screen:

我相信为了写一个测试用例,我需要写一个预请求脚本.

I believe that in order to write a test case, I need to write a pre-request script.

pm.sendRequest({
    url: pm.environment.get("baseURL") + '/document/upload',
    method: 'POST',
    header: [{
        "key": "Authorization",
        "value": pm.environment.get("authorization"),
        "type": "text",
    }],
    body: {
        mode: 'formdata',
        formdata: [{
            "key": "file",
            "type": "binary",
            "src": "C:UsersDesktopapi.pdf"
        }]
    }
}, function(err, res) {
    console.log(res);
}); 

但是,这个方法被命中了两次,有什么想法让它正确并且只命中一次吗?

However, the method is getting hit two times, any thoughts to make it correct and hit only once?

推荐答案

我浏览了文档并弄清楚了问题所在.我在使用 Runner 运行收集时遇到了问题,在找到了一种处理文件上传的方法后,我终于来到了 Newman,这对于这种情况来说似乎很容易.但是,目前还不清楚如何在使用 Runner 运行时上传文件!

I have gone through the docs and figured it out that what is the issue. I was facing issue while running collection using Runner, after searching out a way to handle file uploading, I came to Newman finally, which seem easy for such scenarios. However, it's still unclear how to upload file while running using Runner!

根据上面的评论:

出于安全原因,Postman runner 不支持文件上传直接地.查找 Github 线程这里

Due to security reasons Postman runner doesn't support file uploading directly. Find Github thread here

这篇关于在 Postman 中使用 multipart/form-data 命中方法 2 次上传文件的预请求脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆