-
Hi, I'm looking to get the current request name programmatically. If I console.log the I also tried to log the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I looked into this and it seems that there is no way to get the name easily but you can kind of get around it by reading every file in the current working directory and look for the name there. To do this you will need to turn on the node library fs by going to your the "scripts": {
"filesystemAccess": {
"allow": true
}
} Then in your collection folder add this snippet to the post-script: const fs = require("fs");
const path = require("path");
const dir = path.join(bru.cwd());
const getAllFiles = function(dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function(file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push(path.join(dirPath, "/", file));
}
});
return arrayOfFiles.filter((x)=> path.extname(x)=== ".bru");
};
getAllFiles(dir).forEach(function(file) {
const content = fs.readFileSync(file,'utf8');
let url = new RegExp("(?<=url: )"+req.getUrl());
if (url.test(content))
{
console.log(path.parse(file).name);
}
}); This will read every file in the collection and look for the url inside each .bru file and use it as a lookup key. If there is no string interpolation going on in the url then it will find the name and print it out in the console. Honestly it would probably be better to just add name as a property that you can accessed using |
Beta Was this translation helpful? Give feedback.
-
The solution here doesn't work for me. My collection has multiple Post requests that all have the same Url, but a different request body. This solution will always return the name of the last request in my collection independently of which request is being executed. |
Beta Was this translation helpful? Give feedback.
I looked into this and it seems that there is no way to get the name easily but you can kind of get around it by reading every file in the current working directory and look for the name there.
To do this you will need to turn on the node library fs by going to your the
bruno.json
file in your collection folder and adding this element:Then in your collection folder add this snippet to the post-script: