Skip to content

Undocumented behaviour: changes to collection variables are not persisted if prerequest script ends with skipRequest #14121

@AnnaVernerovaHID

Description

@AnnaVernerovaHID

Is there an existing issue for this?

  • I have searched the tracker for existing similar issues and I know that duplicates will be closed

Describe the Issue

pm.execution.skipRequest() at the end of a pre-request script cancels the effect of the same script on pm.collectionVariables

Steps To Reproduce

Let's have two requests that implement the standard postman way to cycle through an array of variables - initialize on first run of the first request, then keep the array of remaining elements in collection variables, loop back from last request to first if any elements left:

$kind: http-request
name: Cycle start
url: https://postman-echo.com/get
method: GET
scripts:
  - type: beforeRequest
    code: |-
      const collVars = pm.collectionVariables

      console.log("===============================")
      console.log("Cycle start")
      console.log("Got these collVars:", collVars)

      let items
      if (collVars.has("items") && JSON.parse(collVars.get("items") || '[]').length > 0) {
        items = JSON.parse(collVars.get("items") || '[]')
        console.log("collVars")
      } else {
        items = JSON.parse('["A", "B", "C", "D"]')
        console.log("collVars")
      }
      const item = items.shift()
      collVars.set("item", item)
      collVars.set("items", JSON.stringify(items))

      console.log(
        "\nitem:", item,
        "\ncollVars.item:", collVars.get("item"),
        "\nitems:", items,
        "\ncollVars.items:", collVars.get("items"),
      )

      pm.execution.skipRequest()
    language: text/javascript
order: 2000
$kind: http-request
name: Cycle end
url: https://postman-echo.com/get
method: GET
scripts:
  - type: afterResponse
    code: |-
      const collVars = pm.collectionVariables

      console.log("===============================")
      console.log("Cycle end")
      console.log("Got these collVars:", collVars)

      let items = JSON.parse(collVars.get("items") || '[]')
      console.log(
        "\ncollVars.items:", items
      )
      if (items && items.length > 0) {
        pm.execution.setNextRequest("Cycle start")
        console.log("Will continue with next item")
      } else {
        pm.execution.setNextRequest(null)
        console.log("Will reset items and stop")
        collVars.unset("items")
      }
    language: text/javascript
order: 1000

If the pm.execution.skipRequest() is present at the end of the first script (because in this case, both the first and the last requests are just placeholders for the looping logic, so there should be no need to actually make a HTTP request), the overall output to console shows that although the first script thinks it has made changes to collection variables, they are not there at the beginning of the second request:

'==============================='
'Cycle start'
'Got these collVars:', { id: 'ef7c0352-0447-43f2-a039-995391114aa4',
  mutations: { autoCompact: true, stream: [], compacted: {} },
  values: [] }
'collVars'
'\nitem:', 'A', '\ncollVars.item:', 'A', '\nitems:', [ 'B', 'C', 'D' ], '\ncollVars.items:', '["B","C","D"]'
'Skipping this request'
GET https://postman-echo.com/get: {
...
}
'==============================='
'Cycle end'
'Got these collVars:', { id: '8e4f4e13-43ab-4ef9-ac98-06ff43f015de',
  mutations: { autoCompact: true, stream: [], compacted: {} },
  values: [] }
'\ncollVars.items:', []
'Will reset items and stop'

Screenshots or Videos

No response

Operating System

Linux

Postman Version

12.9.5

Postman Platform

Postman App

User Account Type

Signed In User

Additional Context?

Expected behaviour: value of collVars.get("items") should be the same at the beginning of the second request as it is at the end of the first one.

Commenting-out the pm.execution.skipRequest() achieves the correct result (it just means that we need to make an uneeded call to an endpoint).

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions