11 Nov 2022 08:42 AM
We have a HTTP monitor and in the post-execution script of one of the requests, we do a simple response validation.
var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);
if((jsonData.value.length == 0)) {
api.fail("Unexpected response");
}
However this step consistently fails. I have extracted the response from the monitor execution details ad tried to parse in JS runtime(chrome, nodejs) and there JSON.parse works without any problems. I am not sure what is causing this problem on DT as especially in the event section, I only see "Script execution failed" without further context.
Solved! Go to Solution.
13 Nov 2022 07:16 AM
hi prithvi
to be clear. when you do a JSON.parse of the Body, you expect a value with some number
i will assume this number as a int, and will give to this number n value. in this theory will be 1000
so:
value = 1000
this raise a error in javascript because you are using the function lenght. and this function only count strings when you do something like
value.lenght
this will be undefined
you have two options:
1 ) you can use toString hoping that it works will be sometimes it doesnt work
value.toString().lenght = 4
2) you can use a side function inside the postscript to calculate the lenght something like
var nLength = function(n) {
return (Math.log(Math.abs(n)+1) * 0.43429448190325176 | 0) + 1;
}
nLength(value) = 4
for future test remember you can allways use the error to see the full body in the response to know if the problem its for the logic or for the values in the body
some aproach:
var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);
var datastringify = JSON.stringify(jsonData);
if (response.getStatusCode() != 200) {
api.fail("HTTP error:" + datastringify );
}