# Script has error on the server but works in local Wampserver

**URL:** <https://community.postman.com/t/script-has-error-on-the-server-but-works-in-local-wampserver/4022>\
**Category:** Help Hub\
**Tags:** scripts, access\_token\
**Created:** [February 5, 2019, 9:37am UTC](https://community.postman.com/t/script-has-error-on-the-server-but-works-in-local-wampserver/4022 "2019-02-05T09:37:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahdi.temps](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@mahdi.temps](https://community.postman.com/u/mahdi.temps)\
**Post date:** [February 5, 2019, 9:37am UTC](https://community.postman.com/t/script-has-error-on-the-server-but-works-in-local-wampserver/4022/1 "2019-02-05T09:37:01Z")

</div>

Hi  
I’m trying to get the authentication token and save it to an environment variable using Prerequest Scripts. I found this script and customized it for my needs:

> <https://gist.github.com/bcnzer/073f0fc0b959928b0ca2b173230c0669>

It works perfectly on my local WAMPServer but in the main server this error appears in the log:

- code:-101
- data:“username”
- message:“Required field is missing”

It seems that the json part of the request is not sending. But why this works on my local?

This is the code:

```
var auth_url = pm.variables.get("url") + "user/auth";
const TokenExpiresIn = 10000; // Seconds

var echoPostRequest = {
  url: auth_url,
  method: 'GET',
  header: 'Content-Type:application/json',
  body: {
mode: 'application/json',
raw: JSON.stringify(
    {
    	username:"ali",
    	password:"1"
    })
  }
};

var getToken = true;

if (!pm.environment.get('TokenExpireTime') || 
!pm.environment.get('Token')) {
console.log('Token or expiry date are missing')
} else if (pm.environment.get('TokenExpireTime') <= (new Date()).getTime()) {
console.log('Token is expired')
} else {
getToken = false;
console.log('Token and expiry date are all good');
}

if (getToken === true) {
pm.sendRequest(echoPostRequest, function (err, res) {
console.log(err ? err : res.json());
console.log(echoPostRequest);
    if (err === null) {
        console.log('Saving the token and expiry date')
        console.log(res.json())
        var responseJson = res.json();
        pm.environment.set('Token', responseJson.token)

        var expiryDate = new Date();
        expiryDate.setSeconds(expiryDate.getSeconds() + TokenExpiresIn);
        pm.environment.set('TokenExpireTime', expiryDate.getTime());
    }
});
}

```
