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:
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.
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
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.
To Get the Value of a Key data field in a Form Body type Request:
pm.request.body.formdata.get(“desiredKeyVariableName”)
To Set the value of the “DesiredKeyVariableName” into Global Variables:
pm.globals.set(“DesiredGlobalVariableName”,pm.request.body.formdata.get(“desiredKeyVariableName”));
const body = JSON.parse(pm.request.body.raw); // if the request body is JSON
const queryParams = pm.request.url.query; // if you are using query params
Not sure what you mean by variable keys are unknown. Surely you must know what you are sending in the body?