Mocking a REST API — with Response Simulation

Api On Cloud
3 min readJan 11, 2022
Api-On-Cloud logo

In the previous post, we created 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 simulation capability of Api-On-Cloud. Before we begin with the steps, let’s first watch a video demonstration of a mock API capable of simulating different responses

Video demonstration of the mock API we are building in this post

Step 1: Open the /say-hello API in the visual API editor

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.

Image showing the navigation tree for selecting existing REST APIs

Step 2: Add additional responses to the API

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 just below the button.

We have added 10 responses to the /say-hello API

Image showing the additional responses added to the mock API

Step 3: Save the changes made to the API

Scroll to top of the API Editor to save all the changes we have made to the /say-hello API.

Image of Save button to save changed to the API

Step 4: Enable API Response Simulation

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

Image of button that enables API Response Simulation

Step 5: Copy the API URL

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

Image showing the complete API URL and the button to copy the API URL

Begin using the mock REST API with Response Simulation

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 multiple 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

--

--

Api On Cloud

Api-On-Cloud is a no-code platform to build, test and deploy virtual REST APIs. Learn more at: https://www.apioncloud.com