Knowledge Base | Xelon AG

API Response Test with Postman | Xelon AG

Written by Michael Dudli | Aug 24, 2022 6:55:06 AM

We have described in detail the use of Postman as an automatic API monitoring in this article in detail. Now we will show you how you can examine and test the API returns from your backend server more closely (API Response Test with Postman).

Structure of RestAPI requests and responses

RestAPI requests are HTTP requests sent to your backend server using Get, Post, Update and the like. Ideally, you have configured your backend service so that the most useful information is packaged in your API server's response.

As an example, the return after we create a Linux server on Xelon via RestAPI:

The very first thing to note is the status code at the top right. "200" means successful processing of the API request. The most interesting information in the body is certainly the "localdmid" (for Xelon the main identification of a server), as well as, for example, the IP address at the bottom (made unrecognisable).

Checking values in JSON

I can now define in Postman which information is used to recognise whether a request was successful. In our case, we check whether the localvmid exists:

pm.test("Check if localvmid exists", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.device.localvmid).to.be.a("string");
});

The command searches the JSON structure in the body for the localvmid value within "Device". A string is expected. If the answer does not contain a string, the test fails and alerts the developer team via Postman Monitoring.

Reuse values as variables for the API response test with Postman

If a collection is used for monitoring, for example, it is possible that certain JSON returns must be used again in one of the subsequent API requests. For example, if we create a server and then want to check the server status in another call, we need this ID. We can now easily read the localvmid from the return and save it as a variable:

var jsonData = JSON.parse(responseBody);
pm.environment.set("LOCALVMID", jsonData.device.localvmid);
console.log(pm.environment.get("LOCALVMID"))

The localvmid is read out and stored in the environment variable "localvmid". I can now use this variable in the next request.

The complete "Tests" section now looks like this:

Conclusion on the API Response Test with Postman

Postman offers a very good and exciting environment to manage and monitor RestAPI environments and the API response test with Postman is ideal for SaaS start-ups. You can find more information about Postman in our Knowledge Base or on www.postman.com.