Pre request script of upload file with multipart/form-data hitting method 2 times in Postman

独自空忆成欢 提交于 2020-01-25 07:33:06

问题


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:\Users\Desktop\api.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?


回答1:


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!

As per the comments above:

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



来源:https://stackoverflow.com/questions/53886758/pre-request-script-of-upload-file-with-multipart-form-data-hitting-method-2-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!