This book uses the Postman – REST Client extension for Chrome to test our REST service. We use the 0.8.4.16 version that can be downloaded using https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm. This extension is no longer searchable but you can add it to your Chrome using the given link. You can also use the Postman Chrome app or any other REST Client to test your sample REST application:
Let's test our first REST resource once you have the Postman – REST Client installed. We start the Postman – REST Client from either the start menu or from a shortcut.
Once it's started, you can type the Calculation REST URL for sqrt
and value 144
as the path parameter. You could see it in the following image. This URL is entered in the URL (Enter request URL here) input field of the Postman extension. By default, the request method is GET. We use the default value for the request method, as we have also written our RESTful service to serve the request GET method.
Once you are ready with your input data as mentioned above, you can submit the request by clicking the Send button. You can see in the following image that the response code 200
is returned by your sample rest service. You can find the Status label in the following image to see the 200 OK code. A successful request also returns the JSON data of the Calculation Resource, shown in the Pretty
tab in the screenshot. The returned JSON shows the sqrt
, value of the function key. It also displays 144
and 12.0
as the input and output lists respectively:
Similarly, we also test our sample REST service for the calculating power
function. We input the following data in the Postman extension:
http://localhost:8080/calculation/power?base=2&exponent=4
GET
Here, we are passing the request parameters base and exponent with values of 2
and 4
respectively. It returns the following JSON with a response status of 200 as shown in the following screenshot.
Returned JSON:
{ "function": "power", "input": [ "2", "4" ], "output": [ "16.0" ] }
In the following table, all the URLs start with http://localhost:8080
:
Similarly, you could also perform some negative scenarios as shown in the following table. In this table, all the URLs start with http://localhost:8080
:
URL |
Output JSON |
---|---|
|
{ "function": "power", "input": [ "2a", "4" ], "output": [ "Base or/and Exponent is/are not set to numeric value." ] } |
|
{ "function": "power", "input": [ "2", "4b" ], "output": [ "Base or/and Exponent is/are not set to numeric value." ] } |
|
{ "function": "power", "input": [ "2.0a", "a4" ], "output": [ "Base or/and Exponent is/are not set to numeric value." ] } |
|
{ "function": "sqrt", "input": [ "144a" ], "output": [ "Input value is not set to numeric value." ] } |
|
{ "function": "sqrt", "input": [ "144.33$" ], "output": [ "Input value is not set to numeric value." ] } |