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:
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 data.
Extracting Data from a SOAP Response Element
var soap_body = context.response.body; var name_loc = soap_body.indexOf("name"); assert(name_loc >= 0, "name element start location not found");
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.
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");
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.
name element into a new variable,
name_value, and compare it to the name value we expected to receive (
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.
I’ll investigate such possibilities in my next post.