This article is in continuation of my previous article where I have explained about Azure API Management along with importing and testing the API using publisher portal, as well as, calling an API using developer portal. For my previous article, I have provided the link so that you can have a look at it.
In this article, I will explain about using the API Management (APIM) to set a policy on an API so it will return a response. By using this method, it is very much helpful for developers for proceeding both implementation & testing of API Management instance in case the back-end is not available to send real responses.
Prerequisites
Step 1
- Navigate to APIs from API Management service window.
- Now we need to select-> + Add API from the left side menu.
- Then, Select-> Blank API from the list.
- Now we can provide a user-defined name for the display name and the display name will be automatically reflected in the name field too.
- In the Products field, enter for unlimited.
- Now, Select-> Create for creating a test API with no backend.
Step 2
Now, moving to the next step, we need to add operation for testing the API. For that, we need to do the following steps as given below.
- Navigate to the previous step as we created the API in the last step.
- Select-> + Add operation so that the following values can be entered as given from below table.
Display name
|
We can give the user-defined name
|
Name
|
The name will be reflected as the name given in Display name
|
URL
|
We need to provide the URL for the API
|
Description
|
We can give a description of the API which is being used in the developer portal
|
Request
|
We can define request content types such as examples and schemas
|
Response
|
For defining response status code with content types
|
Step 3
- Now switch to the Response tab, from below of Display name & Description fields.
- Then, select + Add response.
- From representations field & select + Add representation.
- Now, we can enter "application/json" inside the search box and enter the {“sampleField”: “test”} in sample text box & then select -> save.
Step 4
Coming to the next step is we need to enable the mock response, so for that,
- Navigate to the API which we created as APItesting.
- Select->test calling in all operations.
- The next step is to select-> +Add policy from the Inbound processing window.
- Then Select -> Mock responses tile in the gallery.
- From API Management response textbox, enter 200 OK, application/json and then select ->save.
Step 5
Now comes the final stage of the process, the final stage is to test the mocked API.
- Now again select API which we created known as APItesting.
- Then switch to -> Test tab.
- Now select-> send for making the test call.
- So, when we click the button a trigger action will be created so that HTTPresponse is displayed in the JSON format.
I hope this article will be useful in mocking API in Azure API management. Thanks for reading!