Setting Optional Path Parameters

If a path parameter is optional, it can be prepended by a colon and then the name of the parameter to indicate it is optional. This will be set as a path variable where the value can be changed in the UI. If pulling in from an environment variable, then the optional parameter can be set and removed via a Pre-Request Script.

var myEnvVar = pm.environment.get("myEnvVar");
var url = pm.request.url.toString();

// Check if the optional parameter should be included
if (!myEnvVar) {
    // Remove the optional parameter from the URL
    url = url.replace(/\/:myEnvVar/, '');
} else {
    // Set the optional parameter if present
    url = url.replace(/:myEnvVar/, myEnvVar);
}

pm.request.url = url;