I am using k6 to check performance for one of our endpoints and the endpoint requires using access token so the script fails very intermittently at line
var obj_accesstoken = JSON.parse(res_accesstoken.body, sleep(1.0));
with error as
ERRO[0004] SyntaxError: EOF
running at parse (native)
default at file:///Users/tripate/Documents/k6_perf_POC/notificationServiceRequest.js:32:33(22)
at native executor=constant-vus scenario=default source=stacktrace
I tried adding sleep after http call and after json parse still the exception happens as mentioned in the thread JSON.parse SyntaxError when running a duration test · Issue #358 · grafana/k6 · GitHub . Below is my code
var payload = { email: username, password: password };
var res_accesstoken = http.post(url, JSON.stringify(payload), { headers: { "Content-Type": "application/json" } }, sleep(1.0));
//console.log("Endpoint response for token is = ", res_accesstoken);
try {
var obj_accesstoken = JSON.parse(res_accesstoken.body, sleep(1.0));
} catch (err) {
//console.error(`res_accesstoken.body could not be parsed as JSON. Received:\n${res_accesstoken.body}`);
var res_accesstoken = http.post(url, JSON.stringify(payload), { headers: { "Content-Type": "application/json" } }, sleep(1.0));
var obj_accesstoken = JSON.parse(res_accesstoken.body, sleep(1.0));
}
var token = obj_accesstoken["data"]["access_token"];
I don’t understand if this is okay because my checks at the end are working expected and no threshold error is shown to user