Set Body-File via Pre-Request-Script?

Hey Guys,

i cant do my download/upload file-requests via collection runner, because i manually have to choose a file in body everytime before.

This leads to two different ideas of getting this problem done:
1: Is it possible to set Body-Key/Value(File) via Pre-Request script?
For example:
Before request, do:
set Value to Key “content”, Value “File - please Choose” to "C:…"
Then do the request
Then do Test

Because if this would be possible i could do my automated testing via Postman Runner without newmanÂĄ

Idea 2 of getting this problem done: Use Newman.

I tried. its Newman run ‘COLLECTION’ -e ‘ENVIRONMENT’ --globals ‘GLOBALS’

I linked my collection, env and globals in it and newman starts its run. My tests in Postman contain a lot of variables which i fill during the test. Every Testcase in newman crashes because every variable (which runs perfectly fine via postman) is “undefined” in newman and the test is failed…

Maybe someone can give me a tip if my idea with the pre-request-script is possible (and how, cause i tried…) and/or where my mistake is with my approach to newman.

Thank you very much!

To Idea/Approach2, here the code (to give a example what doesnt work)_

var request = JSON.parse(responseBody); 
var resultCount = request.resultsCount;

var result1 = ""; 
var result2 = ""; 
var result3 = "";

if (resultCount > 0)
{ 
var result1 = ", " + request.result[0].description; } 

if (resultCount > 1)
{ var result2 = request.result[1].description; } 

if (resultCount > 2) 
{ var result3 = ", ..."; }

pm.test("We got " + resultCount + " results" + result1 +", "+ result2 +""+ result3, function () { 
pm.expect(request.resultsCount).to.be.gt(0);

-> result: “We got undefined results”. So he acts like he doesnt recognize the variables i let him create during the testcases…

What exactly does your responseBody look like ? What is returned ? Can you paste the response here ?

Download-endpoint: Status 200 OK
Body completely empty after download is finished (before it just does nothing )

Upload-endpoint1: Status 200 OK,
Body:
[
{
“id”: XXXXX,
“activityId”: “XXXXX”,
“customerToken”: “XXXXX”,
“contentLocation”: “XXXXX/XXXXX-1030-475f-adcb-ee476208651f.jpg”,
“contentType”: “image/jpeg”,
“contentSize”: 68489,
“content_Hash_MD5”: “XXXXX==”,
“originalFileName”: “berlin.jpg”,
“createDateUTC”: “2018-01-10T15:02:30.543”,
“modifyDateUTC”: “2018-01-10T15:02:30.543”,
“createdByUser”: {
“sid”: “XXXXX”,
“firstName”: “XXXXX”,
“lastName”: “XXXXX”,
“profileVersionStamp”: "XXXXX
},
“modifiedByUser”: {
“sid”: “XXXXX-XXXXX-XXXXX-XXXXX-XXXXX”,
“firstName”: “XXXXX”,
“lastName”: “XXXXX”,
“profileVersionStamp”: “XXXX-XXXX-XXXX-XXX”
}
}
]

Upload-endpoint2: Status 200 OK,
Body:
{
“succeeded”: true,
“errorDescription”: null,
“pictureVersionStamp”: “2XXXXXf7”
}

Thanks in advance!

Looking at the responses, when a JSON responseBody is returned, it does not contain a “resultsCount” variable so therefore … var resultCount would be ‘undefined’.

Ah i didnt explain my problem well i guess.

the code i provided in the first place with a lot of variables was NOT for a upload or download endpoint. this is just a simple “get 200 activities and list them for me” endpoint. which works perfectly fine in postman, and the variables are filled in postman - but not in newman. I would love to know why.

The other problem i have is that i want to automate upload-endpoints in postman (which is technically kinda impossible) but my idea would be to set the file in the request-header (which should be uploaded) via pre-request script. is this possible?

This is a hunch, javaScript is funky compared to straight java … Try this …

var request = JSON.parse(responseBody); 
var resultCount = request.resultsCount;

var result1 = ""; 
var result2 = ""; 
var result3 = "";

if (resultCount > 0) { 
    result1 = ", " + request.result[0].description;
 } 
if (resultCount > 1) { 
    result2 = request.result[1].description; 
} 
if (resultCount > 2) { 
    result3 = ", ..."; 
}

I believe that defining the same variable names using “var” within the “if” constructs causes those variables to be “local” only to the code within the construct so if you get rid of the “var” declaration, it will use the "vars"s that are defined globally to the program.

As far as your “upload” problem, I’m not sure if POSTMAN can handle that depending upon how the endpoint is designed and where it looks for the file to upload.

Here i have to choose manually before the execution of the request. its not possible to set this damn variable (the file-adress) via pre-request script?

You can create a POSTMAN variable such as {{jpgUpload}}

and then in your Pre-request Script

pm.environment.set("jpgUpload", "berlin.jpg");

Hope this is what you were looking for.

1 Like

You can’t use collection runner for file uploads. I created a feature request Collection runner with file uploads .

But you can use Newman ==> http://blog.getpostman.com/2017/09/21/run-collections-with-file-uploads-using-newman/

1 Like