0

In the following postman pre request script, I need to call multiple requests before I make an actual postman request.

Firstly, it will call session API to retrieve the session id

After session id is success only, then I need to call customer signon to make the user logged in.

So I did the following changes in my postman prerequest script but some how the LocalSessionId in the customer signon request is undefined.

const sessionRequestData = {
    url: "http://myurl.com/session",
    method: 'POST',
    header: { 'content-type': 'application/json' },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
    "device": {
        "id": "web",
        "type": "WEB"
    }
    }
};

pm.sendRequest(sessionRequestData, function (err, res) {
   var jsonData = res.json();
    if (err) {
        console.log("mailman: error retrieving session id"+err);
    }
    else {
        console.log("mailman: retrieved session data succesfully"+ jsonData.session.id);
        pm.variables.set("LocalSessionId", jsonData.security.session.id);
       
    }
});


const customerSignonRequestData = {
    url: "http://myurl.com/CustomerSignOn",
    method: 'POST',
    header: { 'content-type': 'application/json' },
    body: {
        mode: 'raw',
        raw: JSON.stringify(  "customerRequest": {
        "identities": {
            "identity": [
                {
                    "data": "sessionId",
                    "value": `${pm.variables.get("LocalSessionId")}`
                }
            ]
        }
    });


 pm.sendRequest(customerSignonRequestData, function(err, res){
     console.log("customer sign on request "+ JSON.stringify(customerSignonRequestData.body.raw))
            var jsonData = res.json();
            if (err) {
       console.log("mailman: unable to signon"+err);
    } else {
        
console.log("mailman: login successful"+ JSON.stringify(jsonData))
    }
        });

Can anyone suggest how can I make sure that the customer signon is called after the session is retrieved only.

Brooklyn99
  • 987
  • 13
  • 24
  • 1
    This post from the Postman community fourm might help here. It explains how this can be achieved in the pre-request script. https://community.postman.com/t/async-operations/24314 – Danny Dainton Nov 24 '21 at 07:29

2 Answers2

0

I had this exact issue. The trick is that you have to nest the second request inside the first. So, it should look something like this:

const sessionRequestData = {
   ...
};
pm.sendRequest(sessionRequestData, function (err, res) {
    ...
    const customerSignonRequestData = {
        ...
    };
    pm.sendRequest(customerSignonRequestData, function(err, res){
        ...
    });
});
elyod72
  • 125
  • 7
0

Here is how I did:


pm.sendRequest({
    url: "http://myurl.com/session",
    method: 'POST',
    header: { 'content-type': 'application/json' },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
            "device": {
                "id": "web",
                "type": "WEB"
            }
        })
    }
}, function (err, res) {
    if (err) {
        console.log("Error retrieving session id: " + err);
    } else {
        console.log("Retrieved session data successfully");
        const sessionId = res.json().session.id;
        pm.sendRequest({
            url: "http://myurl.com/CustomerSignOn",
            method: 'POST',
            header: { 'content-type': 'application/json' },
            body: {
                mode: 'raw',
                raw: JSON.stringify({
                    "customerRequest": {
                        "identities": {
                            "identity": [
                                {
                                    "data": "sessionId",
                                    "value": sessionId
                                }
                            ]
                        }
                    }
                })
            }
        }, function (err, res) {
            if (err) {
                console.log("Unable to signon: " + err);
            } else {
                console.log("Login successful: " + JSON.stringify(res.json()));
            }
        });
    }
});

Brooklyn99
  • 987
  • 13
  • 24