Skip to content

Instantly share code, notes, and snippets.

@prashantagarwal
Last active February 16, 2024 19:20
Show Gist options
  • Save prashantagarwal/f278b3ca78b3927cde06f68674b82cae to your computer and use it in GitHub Desktop.
Save prashantagarwal/f278b3ca78b3927cde06f68674b82cae to your computer and use it in GitHub Desktop.
Script to fetch and reuse a request from a different collection
function executeReferencedRequest({ collectionUId, requestUId, requestName, apiKey }, callback) {
var _ = require('lodash');
let message;
if (!apiKey) {
message = "Pleas generate an API key and add it in your collection level variables"
}
if (!collectionUId) {
message = "Please enter the UId of the collection from which you want to resuse a request or script"
}
if (!requestUId && !requestName) {
message = "Please provide the UId or the name of the request you want to reuse from the collection"
}
if (message) {
throw new Error(message);
}
/*
Here we are making an API call to Postman (Refer: https://api.getpostman.com) using pm.sendRequest from the script,
to fetch the collection from which we need to reuse the request.
*/
pm.sendRequest({
"method": "GET",
"header": [{
"key": "x-api-key",
"value": apiKey,
"type": "text"
}],
"url": {
"raw": `https://api.getpostman.com/collections/${collectionUId}`,
"protocol": "https",
"host": [
"api",
"getpostman",
"com"
],
"path": [
"collections",
collectionUId
]
}
}, function (err, response) {
if(err) {
console.error("Unable to fetch the referenced request", err);
callback && callback(err, null);
throw new Error("Unable to fetch the request with the provided details")
}
let collection = response.json().collection;
/*
This function will find a given request in a collection using request name or id.
It will also inherit Authorization and Scripts from its parent chaing accordingly.
*/
function processCollectionRequest(tree, identifier, identifierType) {
if(tree[identifierType] === identifier){
return tree;
}
else if (!_.isEmpty(tree.item)){
var result;
for(let i = 0; !result && i < tree.item.length; i++) {
result = processCollectionRequest(tree.item[i], identifier, identifierType);
if(result) {
// Extend prerequest and test scripts with collection and folder level scripts
let nodePreRequestScript = _.find(result.event, {listen: "prerequest"}),
parentPreRequestScript = _.find(tree.event, {listen: "prerequest"}),
nodeTestScript = _.find(result.event, {listen: "test"}),
parentTestScript = _.find(tree.event, {listen: "test"});
nodePreRequestScript ?
(nodePreRequestScript.script.exec = _.concat(
parentPreRequestScript && parentPreRequestScript.script.exec,
nodePreRequestScript.script.exec
)) :
(nodePreRequestScript = parentPreRequestScript);
nodeTestScript ?
(nodeTestScript.script.exec = _.concat(
parentTestScript && parentTestScript.script.exec,
nodeTestScript.script.exec
)) :
(nodeTestScript = parentTestScript);
result.event = _.compact(_.concat(nodePreRequestScript, nodeTestScript));
//Inherit authorization from parent chain
if(!result.request.auth && tree.auth) {
result.request.auth = tree.auth
}
}
}
return result;
}
return;
}
//Handle any collection level variables in the referenced collection
_.forEach(collection.variables, function (variable) {
pm.collectionVariables.set(variable.key, variable.value);
});
let identifierType = requestUId ? 'uid' : 'name',
request = requestUId || requestName;
let referencedRequest = processCollectionRequest(collection, request, identifierType),
preRequestScript = _.find(referencedRequest.event, {listen: "prerequest"}),
testScript = _.find(referencedRequest.event, {listen: "test"});
// Store pre-request script, test script & request as local variables so that they can be used later
pm.variables.set("referencedPreRequestScript", preRequestScript && preRequestScript.script.exec.join("\n"));
pm.variables.set("referencedTestScript", testScript && testScript.script.exec.join("\n"));
pm.variables.set("referencedRequest", referencedRequest && referencedRequest.request);
callback && callback(null, collection);
});
}
@srikarnikhil
Copy link

is it possible to reuse the request using the request id rather than the request name? every request has an id for it in postman. I would like to use it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment