When redirected, how to see the URL you´re redirected to?

Hi,

I´m sending a request which returns a 30x and Postman automatically follows the redirect, which is fine.
How can I programmatically “see” the URL I´m redirected to, eg in the test tab?

This has been raised as an issue before (https://github.com/postmanlabs/postman-app-support/issues/319), but as I understand the latest comment, the given solution is not working anymore?
Anyhow, I want to get that value in the test tab, not in some Addon.

Many thanks,
Christian

2 Likes

This has been answered on Twitter: https://twitter.com/ambertests/status/1153709610768859136

After you make the request, the redirect url will be in the temporary request headers

one can access them via request.headers[“referer”]

1 Like

The temporary headers are a great answer.

There is the option of turning off redirects from settings, and using a pre-request script to submit your anticipated redirected urls.

You can then use the redirect information in the 30* header to set your url for the test…

1 Like

pm.request.headers.get("Referer") worked for me in Postman 7.32.0

In PM 10.* you should see this in the console log as a GET http:\xxxx >302 followed by a GET https:\xxx 200.

Using the pm.response.headers object in the test tab of Postman,
you may programmatically get the URL you’re being redirected to.
You can specifically look at the Location header, which typically includes the URL to which you were forwarded following a 30x answer.

Ex:

// Check if the response has a redirect status code (30x)
if (pm.response.code >= 300 && pm.response.code < 400) {
  // Get the 'Location' header from the response headers
  var redirectUrl = pm.response.headers.get('Location');
  console.log('Redirected URL: ', redirectUrl);
}

On this pm.response.headers.get(‘Location’) method retrieves the value of the Location header from the response headers, which should contain the URL you were final redirected to.

You can verify your URL on this tool https://redirectchecker.com to get detail redirection path.