# How to create a pre-request script for login endpoints that returns text token

**URL:** <https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452>\
**Category:** Help Hub\
**Tags:** authentication, pre-request-script\
**Created:** [December 29, 2022, 3:54pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452 "2022-12-29T15:54:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aazizhfc](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@aazizhfc](https://community.postman.com/u/aazizhfc)\
**Post date:** [December 29, 2022, 3:54pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/1 "2022-12-29T15:54:13Z")

</div>

Hello, I have two collections and each of them has one reqest that gets authentication token from separate APIs. The first one works great and it returns a Json with 3 properties.

```auto
{
    "access_token": "XXXXXXXXXXXXX",
    "token_type": "bearer",
    "expires_in": 3599
}

```

For other end point, I use the following pre-request script so I can consume them in one go:

```auto
const getTokenRequest = {
  method: 'POST',
  url: 'https://Example.api/v3/token',
  header: 'Content-Type:application/json',
  header: 'Accept:application/json',
  body: 'grant_type=password&username=username&password=xxxxxxx'
};

pm.sendRequest(getTokenRequest, (err, response) => {
  const jsonResponse = response.json();
  const newAccessToken = jsonResponse.access_token;
  pm.variables.set('Access_Token', newAccessToken);
});

```

Then I use the new variable “Access\_Token” in the authorization tab for other endpoints that require authentication.

The above scenario works great. The second collection is the one I am having issues with. The token endpoint doesn’t return a json token with properties like the above. It just returns the text of the token. The following is the Api token end point

```auto
Method: Post
the url is https://Example.api/session/login
Headers:
                 Accept: application/vnd.v1+json
                 Content-Type: application/json
Body: 
          {"UserId": "userid","Password": "xxxxxxxx"}

```

It returns the token as just text

```auto
sdrlkfjgnsdlfndlfndslfnsdl
     kjhndsffgdflkjnbgdslkfgnSDGSDKNNJGJsdikjngdlfknsdfldksnfs

```

Any help is appreciated.

---

<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:** [December 29, 2022, 4:21pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/2 "2022-12-29T16:21:28Z")

</div>

To get the text response it would just be:

```auto
console.log(pm.response.text())

```

I’m just logging it to the console here but you can save that value to a variable and use it in the same/similar way you have in previous requests.

Maybe something basic like this structure to use in an async request from a script:

```auto
pm.sendRequest({
    url: 'https://Example.api/session/login',
    method: 'POST',
    header: { 
        'headername1': 'value1', 
        'headername2': 'value2' 
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({ UserId: '1234', Password: 'itsASecret' })
    }
}, function (err, res) {
    pm.collectionVariables.set(res.text());
});

```

---

<div class="post-metadata">

**Author:** ![aazizhfc](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@aazizhfc](https://community.postman.com/u/aazizhfc)\
**Post date:** [December 29, 2022, 4:56pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/3 "2022-12-29T16:56:35Z")

</div>

Hi Danny,  
Thank you for you response.  
I tried your suggestion but I am getting an error in the console. See attached image.

 ![postmanError](https://us1.discourse-cdn.com/getpostman/original/3X/8/f/8ff572110d4bc87c40fb5f92c3f5eff3d8eba53e.png)

Any thoughts?

Ali

---

<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:** [December 29, 2022, 5:12pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/4 "2022-12-29T17:12:42Z")

</div>

The request looks like it returned a 400 so I’m guessing that some part of it wasn’t sent correctly.

I have zero knowledge about the request you’re sending, apart from the example you have posted. I don’t know how it’s supposed to be structured.

Check the console and see what was sent in the body of the login request

---

<div class="post-metadata">

**Author:** ![aazizhfc](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@aazizhfc](https://community.postman.com/u/aazizhfc)\
**Post date:** [December 29, 2022, 5:25pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/5 "2022-12-29T17:25:39Z")

</div>

What is strange is that If I call the login endpoint separately, it works and returns a token.

 ![postmanLogin](https://us1.discourse-cdn.com/getpostman/original/3X/d/a/da7a03087b485c6b09245904f48aa9ec3ea28473.png)

Ali

---

<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:** [December 29, 2022, 5:36pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/6 "2022-12-29T17:36:50Z")

</div>

I don’t know what else you have tried in the script. What was the output in the console for the request body?

It’s going to work in the main request because you’re not sending it via the script, which would have a slightly different structure and most of those things been auto done for you already.

- Try adding quotes around the key values?
- Try removing the `JSON.stringify()` the body.

Basically, trial and error. I’m not sat there in front of this so I can only make suggestions of what _I_ might do to solve that situation, I don’t have the ‘_solution_’ 😁

---

<div class="post-metadata">

**Author:** ![jlamota](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jlamota](https://community.postman.com/u/jlamota)\
**Post date:** [December 5, 2023, 9:28pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/7 "2023-12-05T21:28:32Z")

</div>

I was just having issues on trying to do the same thing. Just resolved it with the following script.

```auto
var requestConfig = {
    method: 'POST',
    url: 'myEndpointUrl',
    header: 'Content-Type: application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify({
            "username": "admin",
            "password": "123456"
        })
    }
};

pm.sendRequest(requestConfig, function (err, response) {
    if (err) {
        console.error(err);
    } else {
        if (response.code === 200) {
            var responseBody = response.json();
            var token = responseBody.token;
            pm.collectionVariables.set('accessToken', token);
        } else {
            console.error('Failed to login. Response code:', response.code);
        }
    }
});

```

Hope it helps someone. 🙂

---

<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:04pm UTC](https://community.postman.com/t/how-to-create-a-pre-request-script-for-login-endpoints-that-returns-text-token/42452/8 "2025-04-09T14:04:01Z")

</div>

Closed on April 9, 2025, 2:04pm UTC


