How can I read the content of a form-data request body variable in the test script?

Hello, everybody,

in a test for a REST web service I create a document on the server with a body form-data variable that has its value read in from a file. In the collection JSON this look like this:

"body": {
   "mode": "formdata",
   "formdata": [
      {
         "key": "documentParams",
         "value": "{\n... some parameters ...\n}",
         "type": "text"
      },
      {
         "key": "inputStream",
         "type": "file",
         "src": "/C:/testfiles/content3.txt"
      }
   ]
}

In the test script I read the contents of the document back from the server and now I want to compare what I read with what I have written. I thought the most obvious way to implement this would be to read the value of the contents of the inputStream parameter from the request. I.e. the contents read from the file into this parameter, not the file path.

Yesterday I spent hours to find out how I can get at the request body form-data parameter. I looked here, here and through various forum posts but I could not figure out how to do this.

My guess is something on the line of

pm.request.body.formdata.get("inputStream")

which returns an array of 2 objects. But I can not put it any further.

So, is there anybody here who knows how to get at the value of a request body form-data variable in the test script? Any help would be greatly appreciated.

Thank you.

Frank

Try to reformat Request data as JSON and then with ‘dots’ explore it

var response = pm.response.json();
var req = JSON.parse(request.data); //req because request is already taken as we extract data from it. 
//or 
var req = JSON.parse(request.body);

To get data from request you don’t need pm.*

Then

console.log(request) // if its [object object] try without Json.parse

And see how the format looks like.

Thanks for your answer.

I tried your suggestions in various ways, but to no avail.

Re. var response = pm.response.json();: My question is about the request, so I guess this was a typo.

If I change this to var reqq = pm.request.json(); this yields the error message TypeError: pm.request.json() is not a function.

I experimented a bit and tried pm.request.body.toJSON()which yields [object object].

I am stymied…

That was example to differentiate response from approach of get request

This is not valid.

pm.request.json();

Try simple

console.log(request.data);

If your body is part of the key value form-data param of “body”
then

console.log(request.data.body) 

should print that param.
If postman thinks it is a Json and print [object object] then reformat it

var req = JSON.parse(request.data.body); // or stringify
console.log(req);

and in req you have everything via dots.

I don’t know how you managed to send complex json object with form-data (key: value) ?


I have tried on postman Echo API and this should work to get access to request.data.body

go and grab it https://www.getpostman.com/collections/775ca1bcdb215de4a262

That is really strange: When I put

var req = request.data;
console.log(req.body);
var jbr = JSON.parse(request.data.body);
console.log(jbr);

into my tests just like you I get the following output from the console.log statements:

undefined
JSONError: Unexpected token u in JSON at position 0

After studying the Postman source code I found out that I could read pm.request.body.formdata.toJSON(). Unfortunately that is not the request sent to the server but the definition of the formdata in Postman.

Up to now I had no luck trying to find the source code for request.data.

Can you post a prtScn of body tab? The place where you attached the data that you try to read back??

As you can see in my example, Key in form-data is called ‘body’, but you could name it differently.
That’s why I can call it like

var req = request.data;
console.log(req.body);

Edit :
Try to print

console.log(request.data)

And see key value json of data that was attached in Body tab of postman.