Mocking a REST API

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 needs to call for its data needs 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 access the data which is exposed by the API
  • You do not have full understanding of the relationships between data objects and any 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

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

Step 3: Provider an API Path

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

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

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

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

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

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.

Mock REST API with more than one Response

Click the button below to learn how you can add more responses to this mock REST API to make it more dynamic