Want to Execute Before Invoke script before gRPC Reflection

I want to reflect RPCs from the grpc server, but my server requires authentication with Bearer token.

I’ve set the token in the pm.Collection. However it seems, “Before invoke” script does not executed when request reflection.

Everytime the token expires, I need to update the token in collection myself.

How can I make it automatically updates the token when I request reflection to the server?

Here’s my script:

try {
    const response = await pm.sendRequest({
        url: pm.environment.get("auth_server_url"),
        method: "POST",
        header: {
            'Content-Type': 'application/json',
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify({
                "api_key": pm.environment.get("api_key"),
                "secret": pm.environment.get("api_secret"),
                "scope": pm.environment.get("api_scope"),
            })
        }
    });
    pm.environment.set("jwt_token", response.text())
    console.log("Successfully refreshed JWT")
} catch (err) {
    console.error(err);
}

Hi @mission-geoscienti13 ,

Welcome to the community.

If this is your complete script it looks like you are missing async from your script so force the script to execute asynchronously.

you could do something like this.

(async () => {
    try {
        const response = await pm.sendRequest({
            url: pm.environment.get("auth_server_url"),
            method: "POST",
            header: {
                'Content-Type': 'application/json',
            },
            body: {
                mode: 'raw',
                raw: JSON.stringify({
                    "api_key": pm.environment.get("api_key"),
                    "secret": pm.environment.get("api_secret"),
                    "scope": pm.environment.get("api_scope"),
                })
            }
        });
        pm.environment.set("jwt_token", response.text());
        console.log("Successfully refreshed JWT");
    } catch (err) {
        console.error(err);
    }
})();

Alternatively, use a standard Async function instead of his Immediately Invoked Function Expression (IIFE),