Script has error on the server but works in local Wampserver

scripts
access_token

#1

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:

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());
    }
});
}