Introduce a new Variable Scope - Folder Variable

Hi, I have a use case where I have an API collection that was used for one product (Assume product A). Inside the API collections, there are finer categorization of different API calls categorized by their subpaths as follows:
http://{{host}}:{{port}}/{{subpath}}

where each subpath is categorized by the folders shown in screenshot below.

Each folder in the API collection is referring to a different microservice, and they would have different subpaths, eg.

/ProductA/Auth
/ProductA/Asset

As a user, i also have a lot of other API collections for other products, so it would be best (if possible) if i do not use different API collections for different microservices, since they are all related to a single product in this case.

Hence, imo, it would be best if there’s a finer variable management at folder level as well, so that users can use Collections for storing product based APIs and Folder for storing microservices based APIs.

This already exists, unless I misunderstand your question.

You can set your url for each endpoint to something like

https://{{Product}}/{{FolderName}}

Then, right click on the folder, click on Edit and then in the Pre-Request tab, add this code:

pm.globals.set(“FolderName”, “Auth”); // (or whatever the folder name is).

Do this Pre-Request add for each of your folders, setting the var to the Folder’s name.

Poof. Done.

2 Likes

Thanks @wendy.cook . I just tried this and it seems to work. But i am curious if this means that if the {{FolderName}} needs to be an unique name, because you’re setting this as global variable and if I’m having the same FolderName for different Product, would the variable will be able to resolve correctly.

Eg.
/{{ProductA}}/{[Auth}}
/{{ProductB}}/{[Auth}}

Alright, seems that everytime you run an API from a folder, the Pre-request scripts will always get triggered and this will overwrite the last known global {{FolderName}} variable set. This allows {{FolderName}} to be a non-unique name and can be re-used on some other collections as well.

Thanks for the wonderful tips.

To use a pre-request script amounts to a workaround. Why not have an actual variable set whose scope level is per-folder, in the way that is presently implemented with Globals, Workspaces, and Collections?

5 Likes

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