postman-collection
postman-collection copied to clipboard
Postman.setNextRequest() unable to pass request id
I wrote a script in the test tab to set an access token from the login request. After setting access token I want to call logout API to revoke my session or access token.
I have mentioned .setNextRequest("Requestname" i.e logout) and logout request also included in that folder.
When I run all the folders in the collection runner, I get success to call login request and set access token but my .setNextRequest() method is not working where I am passing the request name.
If we have to pass request ID then I am unable to identify request ID within that folder. Any useful suggestion would be appreciated.
Here is my script in the test tab of the login request.
var Jasondata = JSON.parse(responseBody); var accessToken = Jasondata.payload.accessToken; console.log(accessToken);
pm.environment.set("accessToken", accessToken); postman.setNextRequest("7");
@AfzaalQALhr Can you share the whole Collection JSON file, such that this issue can be resolved faster?
It seems that you are trying to use the setNextRequest() function in Postman to specify the next request to be executed after setting the access token. However, you're currently passing a request name or ID directly, which may not be working as expected.
To address this, you can use the request name or request ID within the folder to set the next request. Here's an example of how you can achieve this:
var jsonData = JSON.parse(responseBody); var accessToken = jsonData.payload.accessToken; console.log(accessToken);
pm.environment.set("accessToken", accessToken);
// Get the request ID or name of the logout request within the same folder var folderId = pm.info.folder.id; var requests = pm.collection.getFolderById(folderId).requests; var logoutRequest = requests.find(request => request.name === "Logout Request"); // Replace "Logout Request" with the actual name of your logout request
// Check if the logout request was found if (logoutRequest) { pm.setNextRequest(logoutRequest.id); } else { console.log("Logout Request not found within the folder."); }
In the code above, we first parse the response body and extract the access token as you did in your original script. Then we set the access token in the environment using pm.environment.set().
Next, we retrieve the folder ID of the current request using pm.info.folder.id. We use this ID to access the requests within the folder using pm.collection.getFolderById(folderId).requests. We search for the logout request by its name (replace "Logout Request" with the actual name of your logout request). If the logout request is found, we set it as the next request using pm.setNextRequest(logoutRequest.id), where logoutRequest.id is the ID of the found request. Otherwise, we log a message indicating that the logout request was not found within the folder.
Make sure to replace "Logout Request" with the actual name of your logout request.
@AfzaalQALhr