Can I use pm.sendRequest() to send requests synchronously?

it helped, thank you!
just created the function as in your comment and call it within 1st pm.sendRequest

I think this post could resolve this issue: Async Operations!

1 Like

Sorry, but did not solve. For example:

/* Common Decrypt Function */
postman.setEnvironmentVariable("utils", () => {
    var decryptRequest = () => {
        var response = JSON.parse(responseBody);

        pm.test("Encrypted response is not empty", function () {
            pm.expect(response).not.be.empty;
        });

        pm.test("Encrypted parameters copied successfully", () => {
            if (pm.expect(response).to.have.property('key')) {
                pm.collectionVariables.set('key', response.key);
                console.info("[key] " + response.key);
            }

            if (pm.expect(response).to.have.property('payload')) {
                pm.collectionVariables.set('payload', response.payload);
                console.info("[payload] " + response.payload);
            }
        });

        pm.test("Decrypted request executed successfully", () => {
            const decryptRequest = {
            url: pm.environment.get("url-platform") + '/client/decrypt',
            method: 'POST',
            async: false,
            crossDomain: true,
            header: {
                    'content-type': 'application/json',
                    'X-Test': pm.collectionVariables.get("xTest")
                },
            body: {
                    mode: 'raw',
                    raw: JSON.stringify({
                        key: pm.collectionVariables.get("key"),
                        payload: pm.collectionVariables.get("payload")
                    })
                }
            };

            let promisify = function request(options) {
                return new Promise(function(resolve, reject) {
                    pm.sendRequest(options, function (err, response) {
                        if (err) return reject(err);
                        setTimeout(resolve(response), 15);
                    });
                });
            }

            async function decrypt() {
                var res = await promisify(decryptRequest);
                if (pm.expect(res).not.be.empty) {
                    var decrypted = res.json();
                    pm.variables.set('decrypted', JSON.stringify(decrypted));
                    console.info("[decrypted] " + JSON.stringify(decrypted));
                } else {
                    pm.variables.set('decrypted', null);
                }
            }

            decrypt();
        });
    }
    return {
        commonPackage: {
            decryptRequest
        }
    };
});

/* Functional */
let utils = eval(environment.utils)();
utils.commonPackage.decryptRequest();

var result = JSON.parse(pm.variables.get("decrypted"));
console.info("[result] " + JSON.stringify(result));

Always return null! It does not run synchronous. Please, any suggestion?

Thank you … Thank you so much … for this solution , it saved my time, i didn’t even think it off…

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.