1

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:

enter image description here

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?

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • It's bound to hit two times since you're already sending the request and you've also written in the test script this script to send another request. During the request execution phase, the pre-request script is executed first, followed by the request itself then finally the test script. So as soon as you hit 'SEND' then both these requests are executed and then the execution finishes. Why are you using pm.sendRequest when you're already sending the api data in the main request itself? – Sivcan Singh Dec 26 '18 at 08:24
  • Yes, 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! @SivcanSingh – Divyang Desai Dec 26 '18 at 08:56
  • 1
    I am aware of that issue. It's due to security reasons that this has been done right now but you can refer the following issue: https://github.com/postmanlabs/postman-app-support/issues/4824#issuecomment-409548624 I have explained why this behaviour is there and we're actively tracking the issue of storing file paths in the app. Newman is the best solution for now. – Sivcan Singh Dec 26 '18 at 09:06
  • @SivcanSingh, okay, thanks for the information! – Divyang Desai Dec 26 '18 at 09:13
  • @SivcanSingh, May I know which framework Postman is using for test script, I see that `.not.eql` or `.not.equal` not working. – Divyang Desai Jan 21 '19 at 06:41
  • Here you go: https://github.com/postmanlabs/chai-postman – Sivcan Singh Jan 21 '19 at 09:33

2 Answers2

4

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

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
0

You can add request before this one in your collection which makes the upload if you need it in the next one. Although the good practice says, that the requests should be atomic with pre-request it will be very difficult. You may achieve it using base64 string of the files and send request with formdata if you insist of doing it like that. The other option runs with Newman in a pipeline. All you have to do is export the collection, the environment and the test files and make sure you don't have absolute path in the exported json. ( Newman should be executed from the directory with the collection and env json files)

A.Sideris
  • 530
  • 3
  • 18