Tuesday 16 May 2023

Postman: How to open a browser window/redirect from a pre-request script?

I have a pre-request script in Postman that retrieves an internal identifier from an API based on a CLIENT_COMMON_NAME variable. To authenticate, I am using my browser cookies with Postman Interceptor and Interceptor Bridge.

The pre-request script works fine when logged in, but for convenience, I want it to redirect me to the internal login page https://auth.example.com if I'm currently not logged in.

My code:

const BASE_URL = "https://auth.example.com";
const API_CLIENT = pm.globals.get("API_CLIENT");
pm.cookies.jar().getAll(BASE_URL, function (error, cookies) {
  pm.sendRequest(
    {
      url: `${BASE_URL}/identitygen?user=${API_CLIENT}`,
      cookies: cookies,
    },
    function (err, res) {
      try {
        err === null &&
          pm.globals.set("TECHNICAL_API_IDENTITY", res.json().identity);
      } catch (error) {
        // How can I redirect to https://auth.example.com here?
      }
    }
  );
});

What would be the best way to redirect to the login page in this scenario? I'm not sure how to achieve this using JavaScript in the Postman pre-request script. Any help would be appreciated.



from Postman: How to open a browser window/redirect from a pre-request script?

No comments:

Post a Comment