Modifying Folder Prerequest and Tests through Postman API

Hello,

I am trying to update a folder’s prerequest and tests through Postman’s API. I can’t find any reference to that in Postman’s API collection, so I tried the same format used with requests, to no avail.

The collection in which the folders are were created with https://api.getpostman.com/collections, which allows to add folder prerequests & tests, because it uses Postman’s collection schema as input. Strangely, every endpoint relating to requests and folders don’t use the same schema structure, which makes it hard to build input data.

The only solution I have found to update folder’s prereq & tests is to replace the whole collection using collection’s put request, but then it wipes & recreates every item in the collection, changing their IDs. Which breaks the collection’s pull features if they were forked in other namespaces, as it causes the whole collection’s content to have changed even if just a single thing has.

Is there a better way?

Thanks,

Hi @mvezina-asyhp. Welcome to the Postman Community.

What you want to achieve can be done using the PATCH request. This lets you update “events” in a collection.

Events in the collection format trigger the running of scripts(pre-request and test scripts).

The PATCH request body looks like this

You must provide the events object specified in the documentation here. The script part of the event can be structured as shown here

As an example, updating an event can have a request body that looks as such:

{
    "collection": {
        "info": {
            "name": "{{collectionName}}",
            "description": "{{collectionDescription}}"
        },
        "events": [
            {
                "listen": "test", // Can as well be 'prerequest'
                "script": {
                    "type": "text/javascript",
                    "exec": [
                        "pm.variables.set('key', 'value)",
                        "console.log('HELLOOO!!!!')"
                    ]
                }
            }
        ]
    }
}

Thank you for your reply.

I believe that the submitted solution though is for Collection prerequests and tests, not those specific to folders, unless there is a way there to target a specific item in the collection tree?

Yes. Both are similar.

A collection can contain an item(a request) or an item group(a folder).

Both an item and and item group use the “item” property according to the spec. What differentiates them is that a “folder” is either an array of “item” objects or an item with and item objects that contains an array of objects.

This part of the documentation talks about the structure of a collection and how items and item groups can be structured.

Both folders and requests (items and item groups) can have events(test of pre request script) and the way you’ll update it is similar or exactly the same way.

Hi @mvezina-asyhp. I realized my last comment wasn’t helpful when reviewing this conversation again. I apologize for the misunderstanding.

When you updated the whole collection using a PUT request, did you maintain the old ID of every item in that collection? Ideally, I will expect it not recreate the items if the ID was provided.

1 Like

No worries.

That’s a great idea. I looked at the JSON schema for Postman Collection Format v2.1.0, and the id prop was missing from folder definition, so I doubted that it’d work. But, I tried it, by providing ids both to items and folders, and it seems to work all right, at least as far as I can see.

What I did is create a collection through POST, and pulled the IDs of all the items in the tree. I forked it in another workspace. I updated a single request in the whole collection and then updated it all through PUT, providing all the items along with their ids. If I go in the fork and attempt to pull changes, the only change I see is the one I made, which is the expected behaviour.

So, this is the solution to the problem. Thanks for your help!

I guess Postman team could update the schema to reflect the fact that folder items also have ids.

As a sidenote, I also tried updating all by providing the ids only for requests (not folders, to follow the schema) and then when pulling changes, all the folders were deleted and recreated, and the requests were marked as moved.

1 Like

This is great to hear! I’m glad you’ve been able to resolve this.

I’ll look into getting the id for folders updated in the schema.

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