The following describes how to run a Virtual User script that will monitor the performance and availability of a SOAP endpoint. The concepts can be extended to any type of API endpoint and for the most part you can probably take this script and modify it a bit to support a REST API endpoint. Here's a breakdown of the code.
First we create a (VU) HTTP client and open the transaction and step which is required to measure the performance:
// SOAP responses are plaintext (XML) and are generally meant to be consumed by another program and not processed by a
// browser so we're going to use the basic HttpClient to monitor, not a Selenium driven browser (i.e. webDriver).
var c = test.openHttpClient();
c.setFollowRedirects(true);
test.beginTransaction();
test.beginStep("Make SOAP Request.");
Two key parameters are the endpoint to be monitored and the message that you want to send. You might need to escape double quotes (\") and will definitely want to remove CR/LF and tabs:
// The URL to our API endpoint and the SOAP message to post
var url = "https://myapi.endpoint.com/path/to/endpoint";
var soapMsg = "<XML_GOES_HERE>";
Now we create and configure our POST request by setting the endpoint URL, the request headers, the request body, and the text we wish to verify in the response. For SOAP the headers will need to include SOAPAction andContent-Type. Sometimes, Content-Length (byte length of the SOAP message) is required.
// Create the POST request and set its configuration
var request = c.newPost(url);
// TODO: Add any other headers that may be required (ex: Content-Length)
request.addRequestHeaders({
'SOAPAction':'[SOAP_ACTION_HERE]',
'charset':'utf-8',
'Content-Type':'text/xml',
'User-Agent':''
});
request.setRequestBody(soapMsg);
request.setVerificationText("[RESPONSE_VERIFICATION_STRING_HERE]");
We execute the request and test the response to see if it matches our verification text. Finally, we close out the step and the transaction.
// Execute the POST and verify the results
var response = request.execute();
test.assertTrue(response.isContentMatched());
test.endStep();
test.endTransaction();
Once the script is complete you can setup a monitoring service just like you would for any other type of monitoring.