In the previous post, we create a mock REST API which responds to HTTP GET requests with a greeting message. In this post, we will further enhance the mock REST API we created previously. We will be adding additional responses to the REST API and switch-on the s imulation capability of the mock API server..
Locate the /say-hello API in the navigation tree on the left. Since /say-hello responds to HTTP GET requests, expand the GET node in the navigation tree. Click on /say-hello to select the API and open it in the editor.
After the /say-hello API is loaded into the editor, scroll down to the Alternate HTTP Responses section of the editor. We will make use of the fields in this section to define the additional API responses. For each response that we add to the API, we will need to provide the HTTP Response Code, HTTP Content-Type, and Response Body.
Press the Save/Add Alternate Response button to add the new response to the table.
We have added 10 responses to the /say-hello API
Scroll to top of the API Editor to save all the changes we have made to the /say-hello API.
We need to enable simulation to make /say-hello return a different response each time it is called by a client application. When simulation is activated, the /say-hello API will pick one of ten responses we added to the Alternate HTTP Responses section.
To activate response simulation, press the Simulation Mode: Off button
To call this mock API, the complete URL of the API must be known. The complete URL is shown in the Complete API Endpoint URL textbox. And the URL can be copied by clicking on the Copy URL button
You can call this mock REST API from your application code or from a command-line program such as cURL.
Please remember that the API will return only one of the ten responses at a time. To view all the responses, you will need to call the API mutilple times. The API Server will randomly pick one of the ten responses and the responses will not be returned in the order in which you have added them
Click the button below for more information on how you can make a mock REST API return different HTTP Status Codes
Next