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 ApiOnCloud in this article.
First let’s identify a few scenarios where mocking an HTTP / REST API can be useful:
Let’s now see how we can create a mock API (or virtual API) with ApiOnCloud to work-around the challenges we have listed above.
Log into the visual API Editor and click on the “Add New API Endpoint”, the first node in the navigation tree on the left
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
Enter the API’s path in the text box shown below
Example: We will enter “/say-hello”
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
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
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
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"
}
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
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.
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.
Click the button below to learn how you can add more responses to this mock REST API to make it more dynamic
next