ERRO[0004] SyntaxError: EOF

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

Hi @ElimaTripathy

Thanks for sharing this in the forum. Have you tried adding the sleep after the calls like this?

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);
} 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"];
sleep(1.0);

If the error does not go away, is it possible for you to share the complete (sanitized) script so we can have a look?

Cheers!

Thanks a lot . I will try as you suggested and let you know

1 Like

Thank You @eyeveebe I tried with adding sleep as you mentioned and it helped to reduce the issue to greater extend but still not completely . Please refer below

Hi @ElimaTripathy

Good to hear we moved a bit forward with this. Now it seems to fail at the imported file backendScripts/NotificationServiceRequest.js. Have you checked that script for similar issues? If you are in doubt kindly share it - best if you can share it in text format, instead of screenshot, as I can more easily test it.

Cheers!