I am receiving JSON string response from target endpoint with Content-Type as “text/plain; charset=UTF-8”. Here is the RAW raw response from target endpoint.
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 194
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 12,
"successful" : 12,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
<br>
I need to reformat(extract an array element or nodeset) the JSON string in javascript and send that as new response.
I am getting following error.
Execution of ConstructResponse failed with error: Javascript runtime error: "SyntaxError: Empty JSON string. (ConstructResponse.js:10)"
Here is my javascript code. I have added javascript step under Proxy Response flow. I am able to get headers count successfully but unable to parse response content as JSON.
print("Res content:"+context.getVariable("response.content"));
print("response.headers.count:"+context.getVariable("response.headers.count"));
var newRes = reconstructRes(context.getVariable("response.content"));
context.setVariable("response.content", newRes);
function reconstructRes(body){
obj = JSON.parse(body);
print("took:"+obj.took);
return JSON.stringify(obj);
}

