Hi Florian,
yes, this was such a frustrating experience.
The entire Postman UI and help seem to indicate that it is both possible and easy using the mechanisms already in place. It seems to suggest you can use existing constructs such as environment variables, which is even displayed on the monitor setup screen. But after nearly 2 weeks of trying and failing and asking support (which was also terrible) I had to give up.
Luckily for me my idea of using a āself-incrementingā real world thing (ie a timestamp) worked instead for my purposes.
In the end, to be fair to Postman, I did get an apology about the sub-optimal support and a full script (see below coz this forum doesnāt let you attach JSON) showing how to actually do this (I never actually tried it).
But Postman really need to sort out the disconnect between their UI and what is actually achievable using Cloud variables.
Here is the JSON example I was givenā¦
{
"info": {
"_postman_id": "2729a578-b6c0-4983-b719-382e120f5382",
"name": "Persist-value-collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "16238262"
},
"item": [
{
"name": "Random Post Data",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"let currentCount = pm.environment.get(\"count\");",
" currentCount = parseInt(currentCount); ",
"",
"//We get the value of the count and parse it to int (Postman stores variable data as strings)",
"pm.environment.set(\"count\", currentCount + 1);",
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"message\": \"Sending message number: {{count}}\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "postman-echo.com/post",
"host": [
"postman-echo",
"com"
],
"path": [
"post"
]
}
},
"response": []
},
{
"name": "Update Environment",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"console.log(`Updated the count. Current count is ${pm.environment.get('count')}` );"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "apikey",
"apikey": [
{
"key": "key",
"value": "X-API-Key",
"type": "string"
},
{
"key": "value",
"value": "{{api_key}}",
"type": "string"
}
]
},
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n // The environment will be updated with the below key/value pairs, anything else will be deleted\n \"environment\": {\n \"name\": \"{{env_name}}\", \n \"values\": [\n {\n \"key\": \"count\",\n \"value\": \"{{count}}\"\n \n },\n {\n \"key\": \"api_key\",\n \"value\": \"{{api_key}}\"\n \n }\n \n ]\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://api.getpostman.com/environments/{{env_id}}",
"protocol": "https",
"host": [
"api",
"getpostman",
"com"
],
"path": [
"environments",
"{{env_id}}"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"key": "env_id",
"value": "16238262-2273310a-8844-46c4-ae49-cd512792bf92",
"type": "string"
},
{
"key": "env_name",
"value": "Test Env",
"type": "string"
}
]
}