Using duplicate query param name to select different values

I am trying to create a request to initialize an environment variable to be used in other requests. The value of this environment variable can be one of five possible values.

To do so, I use a pre-request script that reads the value of the query parameter (thisValue) and sets the environment variable (chosenValue) with that value.

// Set the chosenValue environment variable with this value
pm.environment.set('chosenValue', pm.request.url.query.get('thisValue'));

Next, I made additional query params in the same request, each using the same key. Each instance of the param was set with one of the five possible values.

My expectation was that I could activate one of these five query params and those deactivated would be ignored, sending the value of the active query param to the environment variable via the pre-request script.

Instead, the activated param is used in the request url, but no matter which instance of the query parameter is checked as active, the pre-request script always uses the value of the param at the bottom of the list.

I realize this may be somewhat of a hack, but is there a way of accomplishing this or something similar? I have looked through other threads about being able to select environment variables quickly from a list, but have not seen a solution that seemed to apply.

Perhaps this is simply not possible or best practice. In any case, I appreciate any feedback.

Platform Details:

  • Postman for Windows v10.19.17
  • UI version 10.19.17-ui-231106-0450
  • Desktop platform version 10.19.7
  • Architecture x64
  • OS platform win32 10.0.19045

pm.request.url.query will be an array of objects.
You won’t be able to guarantee which object is returned with the get function.
Although it does appear to return the last object in the list, whether its disabled or not.


But what you can do, is search for the object that doesn’t have the disabled key and return the value from that object.

pm.environment.set('chosenValue', pm.request.url.query.find(obj => obj.key === "test" && !('disabled' in obj)).value); // 123

Thank you for the suggestion. I have replaced the get function and implemented searching for the object that is not disabled in the query array. This solves the problem very nicely!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.