I have a collection of JSON POST requests for the same API. API is the same (same URL) as well as the request structure, only few values are changing within the same JSON body which will change the business scenario. What I wanted to achieve is to store a standard request in a variable and change only affected values within a pre-request script. That has to be dynamic and reusable.
I did configure in the Collection pre-request script the following (I'll make it simple)
const StandardCreateReq = {
"FullName" : "{{FUllName}}",
"ApplicantWebGuid": `${pm.environment.get('APPWEBGUID')}`,
"Address": "Main Street"
};
pm.environment.set("CreateRequest" , JSON.stringify(StandardCreateReq));
Within the request body, instead of writing the full JSON body I'm just configuring it like this:
{{CreateRequest}}
So far so good, it works and I'm able to send the request correctly. What I would like to do is, within the pre-request script of the single request (N.B. not in the pre-request script of the collection) to be able to change few values, for instance either the address with a specific value, or an environment variable like EXISTING_APPWEBGUID instead of the one I'm using which is APPWEBGUID.
I'd need to do this for several requests and I don't want to rewrite the json body everytime and just changing values, I want only within the specific request and as a pre-request action, to be able to change only one or two values. The idea and the difficulty it would be to retrieve the string {{CreateRequest}} and change values in it, I tried with JSON.parse but I'm not able to make it work :-( A possible result would be to send request like following Request 1
{
"FullName" : "John Doe",
"ApplicantWebGuid": "12345",
"Address": "Main Street"
}
Request 2
{
"FullName" : "Jack Brown",
"ApplicantWebGuid": "67890",
"Address": "Main Street"
}
Hope you did get my point and you can help me
Postman Run Collection can do repeat API call with same URL by changing Body content.
Mocking server
Save as
server.jsAddress data
The mocking server will use this address Save as 'address-data.json'
Install server dependencies
Running server
API Test
Get Address URL
Request Application URL
form-data
Loop Test
Save
1-1-demo.postman_collection.jsonImport this collection into Postman
Run collection
Result
Same URL API call with different two people names And result of test logging in Console screen.
Keys
Pre-request ScriptsTab get people informationBody picked up one of people
Teststab checking correct matched user