1

I'm working with an API, which after filling out the log in form on their website, redirects back to our website, with a unique code at the end of the URL.

Example URL after redirect: https://www.mywebsite.com/?code=12431453154545

I have been unable to find a way of viewing this URL in Postman.

Ideally I need to be able to work with that URL to extract the code and store it as a variable.

Any help will be muchly appreciated. I've been trying this all day :( .

  • Does this answer your question? [Is there a way to retrieve the redirected url from a Postman response?](https://stackoverflow.com/questions/56763018/is-there-a-way-to-retrieve-the-redirected-url-from-a-postman-response) – Diogo Rocha Apr 30 '21 at 22:17
  • Thank you, but sadly not. I have tried that and it return an empty string – Mark Powell Apr 30 '21 at 23:02

1 Answers1

1

When you turn off following redirects in Postman settings, you will be able to inspect 3xx HTTP response which will contain Location header with the URL you want to read.

const url = require("url");
var location = pm.response.headers.get("location");
if (typeof location !== typeof undefined) {
  var redirectUrl = url.parse(location, true);
  query = redirectUrl.query;
  if ("code" in query) {
    pm.globals.set("code", query.code);
    console.log(pm.globals.get("code"));
  }
}

Note that this solution will not work when multiple subsequent redirects happen as you will be inspecting only the first 3xx response. You could solve this by following redirects manually and sending your own requests from Postman script as described in Postman manual.

user14967413
  • 1,264
  • 1
  • 6
  • 12
  • Thank you, the first method I had tried and doesn't work, returning the old url. And if I allow the redirect, I get nothing of use in the headers.But that link to the postman manual should be just what I'm looking for. – Mark Powell May 02 '21 at 06:58