Using Javascript to Extract Information from SOAP API Responses

My last post described how to create a monitor that calls Flickr’s Interestingness SOAP API to retrieve a list of recently uploaded photos that Flickr users have found interesting. Once the list of interesting photos is retrieved, we might want to call other Flickr API endpoints to retrieve more detailed information about the individual photos. In order to do this, we’d have to extract from the Flickr Interestingness response body various parameters that would become part of subsequent requests.

In this post, I present a method by which we can extract specific information from SOAP responses using the API Science platform. The earlier post How to Monitor SOAP API Performance queried Flickr’s simple echo test API endpoint: the request includes a name element, and the API echoes back the name element in its response. For this example, I create a monitor that issues the following SOAP request:

The api_key element would be the API key provided by Flickr.

Here, I’ve created a name element that consists of: “Here is what we are looking for”. The expectation is that when this request is sent to the API, the response body will look like this:

Here, I’ve cropped part of the s:Envelope element for clarity. The information of interest is inside the name element. This should match what we sent in the request SOAP package. Note also that the “less than” and “greater than” characters are encoded in the response. We’ll have to account for this fact when we write the Javascript code to extract the name element data.

Extracting Data from a SOAP Response Element

In this example, I create a Javascript validation that examines the response body, extracts the data in the name element, and compares it to the expected value. The first step is to read the response body into a Javascript variable and determine if the characters “name” exist in the body text. Here is the code:

var soap_body = context.response.body;
var name_loc = soap_body.indexOf("name");
assert(name_loc >= 0, "name element start location not found");

Variable soap_body contains the entirety of the response body depicted above. Since this body is text, the Javascript indexOf() function can be used to find the location of the character sequence “name” within the body. The assert() command checks the value of name_loc; if name_loc is less than zero (which means the character sequence “name” does not exist within the soap_body variable), then the validation fails and the check itself is marked as having failed.

If the character sequence “name” exists in the response body, then we assume this is part of the “name” element that is expected to be returned in the Flickr SOAP response. Using the Javascript substring() function, we assign the characters beyond the close bracket to a new variable, name_plus:

var name_plus = soap_body.substring(name_loc + 8);
var close_name_loc = name_plus.indexOf("name");
assert(close_name_loc >= 0, "name element close location not found");

Within the name_plus variable, we again use the Javascript indexOf() function to search for a new instance of the character sequence “name” — which we expect to be the closing HTML sequence for the name element. Again, assert() is called to verify that a second instance of “name” exists within the response body. If this is not the case, then the assert fails, an error message is created, and the entire API check fails.

Given the location of the closing “name” element, we can use the Javascript substring() to extract the data contained within the name element into a new variable, name_value, and compare it to the name value we expected to receive (expected_name):

var name_value = name_plus.substring(0, close_name_loc - 5);
expected_name = "This is what we are looking for";
assert.equal(name_value, expected_name, "Problem");

An assert() verifies that the name value contained in the response matches the expected value (the one we sent to the API in our request). If they don’t match, then the assert fails, and the check fails as well.

Conclusion

In this exercise, we’ve extracted the value from an XML element that is contained with a SOAP response package. Having extracted this data, we can apply additional Javascript validation logic that uses this value as appropriate. For example, in a multi-step monitor, the value received in the response body could be used as an input to a subsequent monitor step.

I’ll investigate such possibilities in my next post.

–Kevin Farnham