0

How can I generate this payload automatically in postman. Please see the structure below.

I am only changing Account Number / Account Name in the payload. If I have add 200 accounts for a customer ID I would have to insert 200 separate structures in the array. Is there a better way to maintain and generate the payload on the fly as I know that only 2 fields are changing and I could pass these as variables. I tried the Collection Runner but I am unable to send these as one request.

FYI - For another corporate it would be 100 or 50 or 500, I wish to use the same payload for any request.

FYI- need to pass all these accounts in one request is because any second request its treated as a modify by the server.

{
  "context": {
    "EID": "ACC_123",
    "eventTime": "2018-01-02T00:00:00.000Z",
    "Eaction": "INIT"
  },
"payload": [
{
"accountNumber": "12345",
"accountName": "abcd",
"accountAliasName": "",
"customerId": "150740000",
"ccy": "THB",
"accountStatus": "A",
"branch": "001",
"bankCode":"0001"
},
{
"accountNumber": "123456",
"accountName": "abcde",
"accountAliasName": "",
"customerId": "150740001",
"ccy": "THB",
"accountStatus": "A",
"branch": "001",
"bankCode":"0001"
}
]

Updated-

Pre-req:

payloadarr.push(
        {
        "accountNumber": Account_Number ,
        "accountName": Account_Name,
...
"context": {
    "eventID": "TEST_JSL_0120191",
    "eventTime": "2018-01-02T00:00:00.000Z",
    "action": "INITIATE"
                },
  "payload": [payloadarr]

Body:

{{AccountActivation_Payload}}
...

Request passed:

{"context":{"eventID":"TEST_JSL_0120191","eventTime":"2018-01-02T00:00:00.000Z","action":"INITIATE"},"payload":[[]]}

Expectation: payload should have the appended values

{"context":{"eventID":"TEST_JSL_0120191","eventTime":"2018-01-02T00:00:00.000Z","action":"INITIATE"},"payload":[{
    "accountNumber": "12345",
    "accountName": "abcd",
    "accountAliasName": "",
    "customerId": "150740000",
    "ccy": "THB",
    "accountStatus": "A",
    "branch": "001",
    "bankCode":"0001"
    },]}
JACOB SAMUEL
  • 3
  • 1
  • 4
  • in Pre-req write `"payload": payloadarr` and in Body are you writing `payloadarr.push(...` ? – ewwink Jan 04 '19 at 11:16
  • No, it was a copy paste issue.Updated it I realize my code is failing because I am using data variables in pre-request screen. – JACOB SAMUEL Jan 04 '19 at 18:13

1 Answers1

0

you can create the following Pre-request Script

var requestBody = {
  "context": {
    "EID": "ACC_123",
    "eventTime": "2018-01-02T00:00:00.000Z",
    "Eaction": "INIT"
  },
  "payload": []
};

for (var i = 0; i < 200; i++) {
  requestBody.payload.push({
    "accountNumber": 12345 + i,
    "accountName": "abcd" + i,
    "accountAliasName": "",
    "customerId": "150740000",
    "ccy": "THB",
    "accountStatus": "A",
    "branch": "001",
    "bankCode": "0001"
  });
}

postman.setEnvironmentVariable('requestBody', JSON.stringify(requestBody));

and it set Environment Variable requestBody then you can call it in the request body using {{requestBody}}

ewwink
  • 18,382
  • 2
  • 44
  • 54
  • The payload is going empty {"context":{"eventID":"TEST_JSL_0120191","eventTime":"2018-01-02T00:00:00.000Z","action":"INITIATE"},"payload":[[]]} . – JACOB SAMUEL Jan 04 '19 at 06:25
  • please add screenshot how you define the `Body` – ewwink Jan 04 '19 at 06:27
  • I have actually replaced the loop variable with datafield variables --------------------------- var Account_Number = pm.iterationData.get("Account_Number") "accountNumber": Account_Number , "accountName": Account_Name, "accountAliasName": "ABC", "customerId": Customer_ID, "bankEntityId":"THAILAND", postman.setEnvironmentVariable('AccountActivation_Payload', JSON.stringify(Tmprequest)); BODY: {{AccountActivation_Payload}} – JACOB SAMUEL Jan 04 '19 at 06:28
  • This is also part of Pre-request script for (var i=0;i>=3;i++) { var Account_Number = pm.iterationData.get("Account_Number"); var Account_Name = pm.iterationData.get("Account_Name"); – JACOB SAMUEL Jan 04 '19 at 06:35
  • please post the code in your question, it less readable in comment – ewwink Jan 04 '19 at 06:37
  • I realize my code is failing because I am using data variables in pre-request screen. that is why the payload code is coming blank. Is there an alternative? – JACOB SAMUEL Jan 04 '19 at 10:25
  • When I changed the File format to JSON, it worked fine. Any reason why the code did not pick values when it was a a CSV file? – JACOB SAMUEL Jan 05 '19 at 14:53