Accessing and modifying request from Scripts

Hello,

I am building a group of tests for my server, so I can keep track of any changes on the response, but I am facing a bit of a problem when it comes to refactoring requests. Right now I have 30 different tests, each of those with 3 to 5 request each, with many of those requests being similar from test to test.

The problem is that my services have changed, and now I need to change those requests, changing the name of some of their json variable for instance. I now realize that, as each request is treated individually, I have to manually change every single request so it can work with my new service. Right now I can do it, but I fear that, as the number of tests keeps growing, changing my request will become more and more difficult.

In others tools I was able to access the content of requests in my collection through a script, and change the content of those requests permanently. That could help me create a script that could refactor my whole project whenever something changed, but I cannot find any way to do this. I can’t find a way to modify the current request on the pre-request script either, which could kind of help me out by checking another requests content and changing the current request to that content.

Am I missing something? Is there any kind of documentation about this?

Have you ever used “globals” or “environment”? Also I wonder how I can use them against my growing requests. I hope to group them by my action parameter and its related parameters.

Hi!

Yes, I have, but I don’t think I can use them to solve this here. Right now I use them for variable values, and it works perfectly for that purpose, but I cannot use them for the whole request, mainly because sometimes I could even need to directly remove or add a variable or part of the request.

Like, this is one of my requests:
57

Here I have both the body, the url, the headers… If I were to, let’s say, add a new field, “business”, I would have to change every single request by hand, which as time goes by will be less and less reliable. The same with deleting fields, changing how they are structured inside the JSON, and so on. And if I were to change the headers, it would be even worse.

Here is what my request “body” looks like …

[
{
“apiKey”:"{{apiKey}}",
“clientDeviceId”:"{{clientDeviceId}}",
“email”:"{{emailAddress}}",
“password”:"{{password}}",
“userName”:"{{userName}}"
}
]

Here is what my “pre-request script” looks like …

postman.setEnvironmentVariable(“clientDeviceId”, “test_client_id_” + Math.floor(Math.random() * 99999));
postman.setEnvironmentVariable(“emailAddress”, "test_client_1234@blah.com");
postman.setEnvironmentVariable(“password”, “password”);
postman.setEnvironmentVariable(“userName”, “test_client_1234”);

I changed my request body to use an environment variable …

[
{{requestBody}}
]

I changed my pre-request script to add the “requestBody” variable …

postman.setEnvironmentVariable(“clientDeviceId”, “test_client_id_” + Math.floor(Math.random() * 99999));
postman.setEnvironmentVariable(“emailAddress”, "test_client_1234@blah.com");
postman.setEnvironmentVariable(“password”, “password”);
postman.setEnvironmentVariable(“userName”, “test_client_1234”);
postman.setEnvironmentVariable(“requestBody”, ‘{“apiKey”:"{{apiKey}}",“clientDeviceId”:"{{clientDeviceId}}",“email”:"{{emailAddress}}",“password”:"{{password}}",“userName”:"{{userName}}"}’);

When I execute the newer version of the API endpoint, it runs just the same as the previous version.

I used an environment variable, but I could have set a “global” variable just the same and that variable would be available to ALL collections that I run. With that, I can add/delete request body variables and the changes will be reflected to all endpoints that use that request body variable.

Sorry for the lack of formatting … hope this helps.