Mocking a REST API

Api On Cloud
4 min readJan 10, 2022
Api-On-Cloud

Mocking allows you to create a simulated version of a REST API. We will go over all the steps needed in creating a mock REST API with Api-On-Cloud in this article.

When to Mock a REST API?

First let’s identify a few scenarios where mocking an HTTP / REST API can be useful:

  • The API your application calls for to obtain data is under development or is unavailable
  • The API your application calls does not return responses that cover all the scenarios, perhaps because the API is running in a Test environment with limited data to work with
  • You do not have direct access to the data and the data is only available via read-only API
  • You have only read-only access to the data
  • You do not have full understanding of the relationships between data objects and data transformations that might be happening before that data is made available by the API to the Client Applications
  • You are looking to a get a specific response from the API which is necessary to test some specific component or logic within your Application

How to Mock a REST API?

Let’s now see how we can create a Mock API (or Virtual API) with Api-On-Cloud to work-around the challenges we have listed above.

Define the REST API Request

Step 1: Creating a new API Endpoint

Log into the visual API Editor and click on the “Add New API Endpoint”, the first node in the navigation tree on the left

Step 2: Pick an HTTP Method for the new API Endpoint

On the “New API Endpoint” screen, select one of the options in the “HTTP Method” dropdown list.

Example: We will pick “GET” from the dropdown list

Image showing HTTP Method selection

Step 3: Provider an API Path

Enter the API’s path in the text box shown below
Example: We will enter “/say-hello”

Image showing the API Path

Define the REST API Response

Navigate to the “HTTP Response” section of the API Editor to define the REST API Response.
Here is how the “HTTP Response” section will look like when we have completed the steps 4, 5 and 6 below

Image showing the values selected for Response Code, Response Content-Type and Response Body fields

Step 4: Configure the HTTP Code for the REST API

Enter the HTTP Response Code in the textbox
Example: We will enter 200 because we want this API to return 200 as the HTTP Response Code

Step 5: Configure the Content-Type of the API Response

Enter the Content-Type in the textbox
Example: We will enter “application/json” in the textbox because we want this REST API to return a response in JSON format to the Caller

Step 6: Provide the contents of the API Response Body

Enter the data that you want this REST API to return when it is called by a Caller Application.
Example: We will enter the following greeting message in JSON format because we want the API to return this exact greeting when it is called by any Application.

{
“greeting”: “Hello”,
“language”: “English”
}

Save the mock REST API

Since we have define the HTTP Request and HTTP Response of the mock REST API, we are ready to save and publish this API

Step 7: Save & Publish the REST API

Navigate to the “Publish API” button to save the changes and publish the REST API. Clicking on the “Publish API” button will make the REST API immediately available for use

Publish API button of the visual API Editor

Step 8: 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 mock API URL and the button to copy the URL

Begin using the mock REST API

You can call this mock REST API from your application code or from a command-line program such as cURL.
Since this is a very basic API that responds to HTTP GET requests, you can also call this mock API from your web-browser by pasting the copied URL in the web-browser’s address bar.

--

--

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