Verify something exists in a response before the extraction is attempted

 
 
var msg = $context.currentItem.previousItem;

var response = msg.getResponse(msg.RESPONSE_HTTP_BODY);

// Was there a response?
if (response == null)
{
// There was no response.

// Output an informational message.
$context.result.postMessage($context.result.LEVEL_INFO, "Message " + msg.name 
 + " received no response to validate.");
}
else
{
// There was a response.

// string to find; hard coded
var stringToFind = "authid";

// Determine if the value exists in the response.
var searchIndex = response.indexOf(stringToFind);

// If the value does not exist in the response, output an error.
if (searchIndex == -1)
{
// authId not found; output error
var details = "Response:\n" + response + "\n\nValue searched for:\n" +  
stringToFind;
$context.result.postMessage($context.result.LEVEL_ERROR, "Expected  value not found in response to Message " + msg.name, details);

// also, set next message to have a play count=1
// the next message is a SignIn message (duplicate of message before  
// this script)
// essentially a retry of the login if it fails

  var nextItem = $context.currentItem.nextItem;
  nextItem.setRepeat(nextItem.REPEAT_TIMING_SERIAL,  
nextItem.REPEAT_TYPE_COUNT_CONSTANT, 1,  
nextItem.REPEAT_DISTRIBUTION_CONSTANT, 0);

}
else
{
// authId is returned, so put it in a clip property

var authId = msg.getResponse(msg.RESPONSE_HTTP_BODY_AS_JSON, "// authid")[0];
$context.result.postMessage($context.result.LEVEL_INFO, "authId: " +  
authId);
$prop.set("MessageClip", "authId", authId);

var nextItem = $context.currentItem.nextItem;
nextItem.nextItem.setRepeat(nextItem.REPEAT_TIMING_SERIAL,  
nextItem.REPEAT_TYPE_COUNT_CONSTANT, 0,  
nextItem.REPEAT_DISTRIBUTION_CONSTANT, 0);

}
}

msg.clearResponse();