# Accessing Request Values from the Console

**URL:** <https://community.postman.com/t/accessing-request-values-from-the-console/52080>\
**Category:** Help Hub\
**Tags:** request, pre-request-script\
**Created:** [September 20, 2023, 2:45pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080 "2023-09-20T14:45:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviation-explorer-21](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aviation-explorer-21](https://community.postman.com/u/aviation-explorer-21)\
**Post date:** [September 20, 2023, 2:45pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080/1 "2023-09-20T14:45:22Z")

</div>

Hello,

I have a pre-request script written in Postman. One of the calls redirects you to a different url as part of the url and I am wanting to access this url. I can see it in the console, but I have no idea how to access it in the script itself. You can see two gray urls, the first is the “codeRequest” that is the request that is being sent and the second is the url that it is redirected to after a successful request. Basically, I want to get the code value out of the second gray url. How can I access this value/url?

 ![Screen Shot 2023-09-20 at 10.39.56 AM](https://us1.discourse-cdn.com/getpostman/original/3X/4/d/4d423882fb2e7e6c329cc8957e475d8cb8ed33ea.png)

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 22, 2023, 8:28pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080/2 "2023-09-22T20:28:10Z")

</div>

hi @aviation-explorer-21

Is it returned in the response header as a redirect or anything like that?  
If you can grab it from the response header you could do something like:

```auto
let redirectUrl = pm.response.headers.get('Location'); // Change to actual header key
pm.environment.set('YourRedirectVariable', redirectUrl);

```

---

<div class="post-metadata">

**Author:** ![aviation-explorer-21](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aviation-explorer-21](https://community.postman.com/u/aviation-explorer-21)\
**Post date:** [September 25, 2023, 1:13pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080/3 "2023-09-25T13:13:49Z")

</div>

I unfortunately tried that but when I try to get the headers out of the response, it is just the response of the API that I initially called and it has no info about the redirect URL that I want. What you suggested is exactly what I want to do though!

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 25, 2023, 5:07pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080/4 "2023-09-25T17:07:16Z")

</div>

Go to settings (within your API call - next to test tab), turn off automatically follow redirects…

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/6/063ab4482f8cef6a36c5e9a1289ef99fe014feed.png)

Then something like this should work:

```auto
//Get all headers
var responseHeaders = pm.response.headers;
console.log(responseHeaders);

//Get specific header value - based on key name
var headerValue = responseHeaders.get("location");
console.log(headerValue);

```

I tried this just on a redirect and it gives me the URL I was looking for…

Hope it helps.

---

<div class="post-metadata">

**Author:** ![aviation-explorer-21](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aviation-explorer-21](https://community.postman.com/u/aviation-explorer-21)\
**Post date:** [September 25, 2023, 5:23pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080/5 "2023-09-25T17:23:26Z")

</div>

Yes this does work if you are doing a regular postman API call, but I was doing everything in a collection level pre-request script to grab authorization from our authorization server automatically instead of having to put the authorization API calls into a folder and manually running the folder each time. However, since I couldn’t find a solution to grabbing the redirect URL in the pre-request script, I have to run the scripts manually. I just wish there was a way to run the folder in the pre-request script.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 25, 2023, 7:24pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080/6 "2023-09-25T19:24:54Z")

</div>

Ah, I didn’t realise it was from a prereq.

You could try setting ‘followRedirects: false’ as a request option.  
I didn’t have much luck on my own test, but from what I read it ‘sometimes’ works depending on the config of the API.

Alternatively, could you use the OAuth 2.0 option within the Auth tab?

> **[OAuth 2.0 | Postman Learning Center](https://learning.postman.com/docs/sending-requests/authorization/oauth-20/#using-oauth-20)**
>
> OAuth 2.0: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

All of the information you are feeding in to your prereq script could be input here (at collection level) instead.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/9/c9d6974b5daf8d3aac31be59ebb3b6108ca5b173.png)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 25, 2023, 7:25pm UTC](https://community.postman.com/t/accessing-request-values-from-the-console/52080/7 "2023-10-25T19:25:06Z")

</div>

Closed on October 25, 2023, 7:25pm UTC

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