I have an environment variable as a mock response and every time I update the environment variable value, I need to persist it to be able to reflect it as a response form the mock server.
Is there some way I can automate this (persist process) so that I can run all my requests at a time in the collection runner.
Thanks for your response,
I am able to set the environment variable value, what I want to do is to persist the variable value, i.e. have both āinitialā and ācurrentā value updated. And I want to do this via code/script, rather than clicking on the āpersist allā button. I hope I made the scenario more clear.
I somehow missed this in the help center. The problem is now solved.
Sending the PUT request with the details of the same environment every time I update the Environment variableās value. Working well.
Hi @bbahadur . I tried this solution and it works fine. Except that if I only wanted to update a subset of variables, the initial values of all the other variables that I did not want to update get deleted.
For example, letās say I have 10 different environment variables. In my script, I only include var1, var2, var3 to persist that current value to the initial values. After the script runs, I see that the initial values of var1 to var3 gets updated correctly. But the initial values of var4 to var10 gets blanked out.
Is there a way to do selective updating of initial values, without affecting all the other variables that I do not want to update?
Iāve done further research, and it seems that there is no direct way to persist current values to initial values of only a subset of variables. The only way I could address this is to get all current environment variables, then update the values of the target subset. After which, I will pass the complete set of environment to the Postman API to persist the values.
Here is an example of what I have created in Github Gist:
@bbahadur Regarding āProgrammatically updates initial values from pre-request or test scriptsā you mentioned it was resolved by sending PUT method request. Can you please explain with detailed steps on how to configure this automatic persist?
Itās been long since I worked on it, but as far as I remember, I send a PUT method request with the details of the environment ID, X-API-KEY and a body that has an environment object and data to be persisted, hereās the structure:
const requestOptions = {
url: 'https://api.getpostman.com/environments/[Env ID here]',
method: 'PUT',
header: {
'X-API-Key':'[API Key here]'
},
body: {
mode: 'raw',
raw: JSON.stringify({
'environment': {
'name': '[Env name here]',
'values': [
{
'key': '[key name]',
'value':[value]
}
]
}
})
}
}
//The request is ready to send, below method sends the request to POSTMAN and saves the variable in the environment
pm.sendRequest(requestOptions, (error, response) => {
if (error) {
console.log(error)
}
})