This article is the third part of a multi-part series. In this section, I will explain how to render custom mock responses from JSON files stored in a storage container. Below are the different parts of this article series.
- Mocking Custom Responses with Azure API Management – Simple Mock Response
- Mocking Custom Responses with Azure API Management – Custom Mock Response
- Mocking Custom Responses with Azure API Management – Custom Mock Response from External Files
{
"id": 1,
"name": "Introduction to Azure",
"category": "Cloud Computing",
"description": "A beginner-friendly course covering the fundamentals of Microsoft Azure.",
"duration": "4 weeks",
"instructor": "John Doe"
}
<inbound>
<base />
<choose>
<when condition="@(context.Request.Url.Query.GetValueOrDefault("id") == "1")">
<send-request mode="new" response-variable-name="response" timeout="10" ignore-error="false">
<set-url>@($"https://rgstoragefedora001.blob.core.windows.net/data/Subject/1.json?sp=r&st=2024-03-08T12:01:42Z&se=2024-03-08T20:01:42Z&spr=https&sv=2022-11-02&sr=b&sig=DrqWZJxGP38PNJYFKCoMgIqn6AjNC71nw0x%2FjITu4aM%3D")</set-url>
<set-method>GET</set-method>
</send-request>
<return-response>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@(new JObject(((IResponse)context.Variables["response"]).Body.As<JObject>()).ToString())</set-body>
</return-response>
</when>
<when condition="@(context.Request.Url.Query.GetValueOrDefault("id") == "2")">
<send-request mode="new" response-variable-name="response" timeout="10" ignore-error="false">
<set-url>@($"https://rgstoragefedora001.blob.core.windows.net/data/Subject/2.json?sp=r&st=2024-03-08T12:34:15Z&se=2024-03-08T20:34:15Z&spr=https&sv=2022-11-02&sr=b&sig=OVhf3uGIASMHWHtg8F%2BypQzLatI9DeEzqoFns2iGOUk%3D")</set-url>
<set-method>GET</set-method>
</send-request>
<return-response>
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@(new JObject(((IResponse)context.Variables["response"]).Body.As<JObject>()).ToString())</set-body>
</return-response>
</when>
<otherwise>
<return-response>
<set-status code="404" reason="Not Found" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"error": "Student ID not found"
}</set-body>
</return-response>
</otherwise>
</choose>
</inbound>