admin管理员组

文章数量:1356849

I Have an Authenticate post call to the server that looks like this :

http://localhost/ServiceName/AuthenticateUser

with a body sent like this:

{
    "userCredentials":"{{securityToken}}"
}

I always have to execute this Authenticate call twice in Postman to get my global var 'securityToken' populated properly and used thereafter for the next calls to Authenticate, so it seems the pre-request script is actually running AFTER the script, or is it that Global vars set in pre-request scripts are not readily available to the current request?

The first time I run this the server returns a login error and the next time it logs in fine.

What am I doing wrong?

Here's the pre-request sript:

// Import the CryptoJS library with jQuery
$.when(
    $.getScript( ".1.2/build/rollups/md5.js" ),
    $.getScript( ".1.2/build/rollups/aes.js" ),

    $.Deferred(function( deferred ){
        $( deferred.resolve );
    })
).done(function(){

    //The scripts are all loaded
    var api = {

        connection: {

            aesIV: 'blabla',
            aesKey: 'secretNoTellingYou'
        }
    }

    var aesIV = CryptoJS.enc.Hex.parse(api.connection.aesIV);
    var aesKey = CryptoJS.enc.Utf8.parse(api.connection.aesKey);

    if (!CryptoJS || !CryptoJS.AES || !CryptoJS.MD5) {
        alert('CryptoJS AES and MD5 Library Must Be Loaded');
    }

    var encrypt = function (text) {
        var encrypted = CryptoJS.AES.encrypt(text, aesKey, { iv: aesIV });
        return encrypted;
    };

    var encryptedUserCode = encrypt(globals["userCode"]).toString();
    var md5Password = CryptoJS.MD5(globals["password"]).toString().toUpperCase();
    var encryptedPassword = encrypt(md5Password.toString());
    var token = SomeFunctionToCreateToken(encryptedUserCode , encryptedPassword);

    postman.setGlobalVariable('securityToken', token);    

});

I Have an Authenticate post call to the server that looks like this :

http://localhost/ServiceName/AuthenticateUser

with a body sent like this:

{
    "userCredentials":"{{securityToken}}"
}

I always have to execute this Authenticate call twice in Postman to get my global var 'securityToken' populated properly and used thereafter for the next calls to Authenticate, so it seems the pre-request script is actually running AFTER the script, or is it that Global vars set in pre-request scripts are not readily available to the current request?

The first time I run this the server returns a login error and the next time it logs in fine.

What am I doing wrong?

Here's the pre-request sript:

// Import the CryptoJS library with jQuery
$.when(
    $.getScript( "http://crypto-js.googlecode./svn/tags/3.1.2/build/rollups/md5.js" ),
    $.getScript( "http://crypto-js.googlecode./svn/tags/3.1.2/build/rollups/aes.js" ),

    $.Deferred(function( deferred ){
        $( deferred.resolve );
    })
).done(function(){

    //The scripts are all loaded
    var api = {

        connection: {

            aesIV: 'blabla',
            aesKey: 'secretNoTellingYou'
        }
    }

    var aesIV = CryptoJS.enc.Hex.parse(api.connection.aesIV);
    var aesKey = CryptoJS.enc.Utf8.parse(api.connection.aesKey);

    if (!CryptoJS || !CryptoJS.AES || !CryptoJS.MD5) {
        alert('CryptoJS AES and MD5 Library Must Be Loaded');
    }

    var encrypt = function (text) {
        var encrypted = CryptoJS.AES.encrypt(text, aesKey, { iv: aesIV });
        return encrypted;
    };

    var encryptedUserCode = encrypt(globals["userCode"]).toString();
    var md5Password = CryptoJS.MD5(globals["password"]).toString().toUpperCase();
    var encryptedPassword = encrypt(md5Password.toString());
    var token = SomeFunctionToCreateToken(encryptedUserCode , encryptedPassword);

    postman.setGlobalVariable('securityToken', token);    

});
Share Improve this question edited Jan 8, 2020 at 7:59 Ian Kemp - SE killed by LLMs 29.9k21 gold badges124 silver badges162 bronze badges asked Aug 3, 2015 at 22:08 zukantazukanta 2,6933 gold badges20 silver badges25 bronze badges 2
  • Could it be possible that on the initial call there is an error somewhere along the way. I had a similar issue where it seemed as if the pre-request script was not running and it turned out that on the server side my service was not correctly activating. Have you tried a tool like Fiddler to inspect the response on your initial request? – GargantuanTezMaximus Commented Aug 3, 2015 at 22:37
  • @GartuanTezMaximus no, tracing the server code, the call is really made with the prior param value and pletes OK. – zukanta Commented Aug 4, 2015 at 20:11
Add a ment  | 

2 Answers 2

Reset to default 4

Actually for me the problem disappeared once I replaced the script imports with the now integrated libraries:

//The scripts are all loaded
    var api = {

        connection: {

            aesIV: 'blabla',
            aesKey: 'secretNoTellingYou'
        }
    }

    var aesIV = CryptoJS.enc.Hex.parse(api.connection.aesIV);
    var aesKey = CryptoJS.enc.Utf8.parse(api.connection.aesKey);

    if (!CryptoJS || !CryptoJS.AES || !CryptoJS.MD5) {
        alert('CryptoJS AES and MD5 Library Must Be Loaded');
    }

    var encrypt = function (text) {
        var encrypted = CryptoJS.AES.encrypt(text, aesKey, { iv: aesIV });
        return encrypted;
    };

    var encryptedUserCode = encrypt(globals["userCode"]).toString();
    var md5Password = CryptoJS.MD5(globals["password"]).toString().toUpperCase();
    var encryptedPassword = encrypt(md5Password.toString());
    var token = encryptedUserCode + "|" + encryptedPassword;

    postman.setGlobalVariable('securityToken', token);    

I can confirm this behaviour, proved by fiddler. The request is fired regardless of the execution of the pre request script.

Apparently this is due to Postman's support of ajax calls in pre request scripts.

https://github./postmanlabs/postman-app-support/issues/644

They remend creating two requests, an initial POST to create the token, and then chaining that with subsequent requests and setting environment variables to sign those requests.

本文标签: javascriptPostman prerequest script executing AFTER the requestStack Overflow