Flow & PreRequest CryptoJS.enc.Base64

Hi everybody!

Findout this great tool “Postman Flows”, but got problem with it.

I tried to use this in my project, set up authorisation chain, but i got stuck in send next request after authorisation.

In normal ways in last step in auth i get sessionkey and than in prerequest script next request i use this

// to replace all variables in {{...}} of the text object
function multiReplace(obj){
    // console.log(obj);
    if (~obj.indexOf("{{")) {
        var var1, var1End, var1Name, var1Value;
        var var1Start = -1;
        while ((var1Start = obj.indexOf("{{", var1Start + 1)) != -1) {
            var var1End = obj.indexOf("}}", var1Start) + "}}".length;
            var var1 = obj.slice(var1Start, var1End);
            var var1Name = obj.slice(var1Start, var1End).replace(/}}/, "").replace(/{{/, "").trim();
            var var1Value = pm.variables.get(var1Name);
            // console.log(var1Value);
            obj = obj.replace(var1, var1Value);

        }
    }
    // console.log(obj);
    return obj;
}

var path = pm.request.url.getPathWithQuery();
// to get post-request path
path = multiReplace(path)
// console.log("path: ", path);

var body = pm.request.body.toString();
// to get post-request body
if (body) {body = multiReplace(body)};
// console.log("body: ", body);

var crypto = CryptoJS.enc.Base64.parse(pm.variables.get("SessionKey"));
// console.log("SessionKey: ", pm.variables.get("SessionKey"));
if (body.length) {
    signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(path + '\n' + body, crypto));
} else {
    signature = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(path, crypto));
}
console.log("signature: ", signature);

// Setting collection and environment variables
pm.collectionVariables.set("signatureHeader", signature);

and this signatureHeader generated in every request after auth and used in Headers.

Can anybody help and explain, how i can generate and use it in Postman Flows?

Hi @kochva

This isn’t possible just yet in Flows. We don’t support updating collection or environment variables from a script and then using the value in Flows.

What I can share is that you will be able to achieve the above in the evaluate block in Flows after Post/Con (next week).

I’ll come back and update my answer here after that’s available :slightly_smiling_face:

2 Likes

Thanks for the answer. I’ll be looking forward to it.

Updating variables is not so important for execution (although they are convenient).
The main goal is to receive correct responses to requests with their correct execution. But the method is not so important for now.