# Postman pre-request script for Auth, actual POST call returns Invalid Token

**URL:** <https://community.postman.com/t/postman-pre-request-script-for-auth-actual-post-call-returns-invalid-token/12566>\
**Category:** Help Hub\
**Tags:** variables, authentication\
**Created:** [May 12, 2020, 1:59am UTC](https://community.postman.com/t/postman-pre-request-script-for-auth-actual-post-call-returns-invalid-token/12566 "2020-05-12T01:59:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyfiel](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jeremyfiel](https://community.postman.com/u/jeremyfiel)\
**Post date:** [May 12, 2020, 1:59am UTC](https://community.postman.com/t/postman-pre-request-script-for-auth-actual-post-call-returns-invalid-token/12566/1 "2020-05-12T01:59:34Z")

</div>

forgive my lack of tests and error logic. I know my pre-request script is working but for the life of me I can’t figure out why the subsequent POST request fails with “Invalid token”.  
I’m using Postman to execute a pre-request script for auth and it’s returning 200 and is giving the { id } from the res. BUT!! when I set the env variable of { token } and then call on { token } from the header of the actual POST request, I’m getting an “Invalid token” response, 401.

I checked the logs and the POST request header is matching the { token } that was returned in my pre-request script. Why would  
the server reject it?

```javascript
const auth = pm.environment.get('auth')
const user = btoa(`${pm.environment.get('username')}:${pm.environment.get('password')}`)
if (!auth) console.log('Missing authorization endpoint')
if (!user) console.log('Missing credentials')
if (pm.environment.get('token')) pm.environment.set('token', '')

const echoPostRequest = {
  url: auth,
  method: 'POST',
  header: `Authorization: Basic ${user}`
};

pm.sendRequest(echoPostRequest, function (err, res) {
  const { id } = res
  pm.environment.set("token", id)
});

```

[pre-request-response](https://i.stack.imgur.com/5CJe1.png)

[post-2nd-request-invalid token](https://i.stack.imgur.com/tnLbc.png)

---

<div class="post-metadata">

**Author:** ![joshuaf91](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/joshuaf91/32/6400_2.png) [@joshuaf91](https://community.postman.com/u/joshuaf91)\
**Post date:** [November 18, 2020, 12:31am UTC](https://community.postman.com/t/postman-pre-request-script-for-auth-actual-post-call-returns-invalid-token/12566/2 "2020-11-18T00:31:33Z")

</div>

I came up with this same idea for a project at work. This was the first post I found and just wanted to provide some clarity for those who land on this page after their epic google search for an answer. 😃

Here is a link to more PM examples on a Gist I found. [https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990](https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990)

Make sure to use `res.json()` that will provide access to the JSON expected vs accessing the Response Object.

Example:

```
pm.sendRequest(echoPostRequest, function (err, res) {
    const json = res.json() // Get JSON value from the response body 
    pm.environment.set("token", json.token) // makes sure to update this key name.
});

```

same idea for a ran into this same issue and wanted to leave a comment here for anyone else that is looking for a solution to this.

since this was the first post I found when looking for a solution

---

<div class="post-metadata">

**Author:** ![navigation-astrono10](https://avatars.discourse-cdn.com/v4/letter/n/898d66/32.png) [@navigation-astrono10](https://community.postman.com/u/navigation-astrono10)\
**Post date:** [August 7, 2022, 6:20pm UTC](https://community.postman.com/t/postman-pre-request-script-for-auth-actual-post-call-returns-invalid-token/12566/3 "2022-08-07T18:20:17Z")

</div>

I am attempting to use this same structure to scrape the token from the pre-request script body. However, I cannot get the “token” variable to pass through to the bearer authentication. When viewing the console I can see the token is correct in the body, but the token variable stays “null”. Any thoughts? Thanks!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 9, 2025, 2:02pm UTC](https://community.postman.com/t/postman-pre-request-script-for-auth-actual-post-call-returns-invalid-token/12566/4 "2025-04-09T14:02:31Z")

</div>


