Rakesh
October 7, 2020, 7:09pm
1
How to parameterized the JSON payload for post requests with multiple variables. e.g. my JSON Is like as follow
{
"oHeader": {
"sReqType": "application/json",
"sAppSource": "WEB 2.12.02",
"sSourceID": "",
"sAppID": "",
"sProduct": "PL",
"dtSubmit": "2020-09-28T16:09:47.574Z",
"sCroId": "croID",
"sDsaId": "dsaID",
"sInstID": 4032,
"sBranchCode": "5",
"sLoginId": "loginId",
"sLoginRole": "role",
"instituteName": "Test",
"sDealerId": "dealerID"
},
"sRefId": "refID",
}
I need to parameterized login, loginRole , refID . I am switching from Gatling to k6 there in Gatling using session variable it was possible to update data.
You can just save this as a JS object, change whatever you want for every request, and then use JSON.stringify()
to serialize it as the body. Here’s an example:
import http from 'k6/http';
import { sleep } from 'k6';
let data = {
"oHeader": {
"sReqType": "application/json",
"sAppSource": "WEB 2.12.02",
"sSourceID": "",
"sAppID": "",
"sProduct": "PL",
"dtSubmit": "2020-09-28T16:09:47.574Z",
"sCroId": "croID",
"sDsaId": "dsaID",
"sInstID": 4032,
"sBranchCode": "5",
"sLoginId": "loginId",
"sLoginRole": "role",
"instituteName": "Test",
"sDealerId": "dealerID"
},
"sRefId": "refID",
};
export default function () {
data.oHeader.sLoginId = "foo";
data.oHeader.sLoginRole = "bar";
data.sRefId = "123";
let response = http.post("https://httpbin.test.k6.io/anything", JSON.stringify(data));
console.log(response.body);
}
Rakesh
October 8, 2020, 7:20am
3
Thanks, Ned. Will check it out