Mocking API responses in Azure API Management
Introduction
In today's API-driven software architecture, developers often face a unique challenge: designing the API façade ahead of its backend implementation. This blog delves into this predicament and offers a practical solution to maintain momentum in such situations.
The challenge
In many software projects, there's a phase where the API façade is ready, but the backend service fulfilling its contract is still under construction. When using Azure API Management Service in such a context, a challenge emerges: without a backend, APIM consistently produces 500 HTTP errors. This can disrupt frontend or client-side development that relies on any form of API responses, even if they're just mock data.
The solution
Azure API Management offers a mocking response feature, precisely for situations like these. When you design an API, but the backend isn't ready, you can set up mock responses for your operations. This ensures that any consumer of the API receives a consistent, pre-defined response, aiding in parallel development and testing.
Crafting a placeholder API
We will create a simple HTTP API that doesn’t need a pre-existing backend.
- Start by signing into the Azure portal and then making your way to your specific API Management instance.
- Opt for
APIs
, followed by+ Add API
and thenHTTP tile
.
Define the blueprint of the API
- Upon the appearance of the "Create an HTTP API" panel, select the
Full
option. - Designate the "Display name" as
Test API
. - For the "Products", opt for
Unlimited
. - Ensure that the
Managed
option is chosen under "Gateways". - Finalize by clicking on
Create
.
Enriching the API with an operation
Every API exposes operations. Here, we'll add one to our HTTP API.
- First, select the previously created API.
- Click on
+ Add Operation
. - In the dedicated "Frontend" panel, provide:
- "Display name":
Test call
. - "URL (HTTP verb)":
GET
. - "URL":
/test
. - Optionally, add a brief "Description" for clarity.
- Jump to the
Responses
tab to outline response status codes, content types, and more. - Click
+ Add response
and choose200 OK
. - Under the "Representations" banner, select
+ Add representation
and opt for theapplication/json
content type. - In the "Sample" field, jot down
{ "sampleField" : "sampleValue" }
. - Save these configurations.
Activating mock responses
Azure API Management's mock response feature truly shines here:
- Revisit the API you crafted in step one.
- On the right-hand side panel, activate the
Design
tab. - Highlight the test operation you appended earlier.
- Within the "Inbound processing" section, go for
+ Add policy
. - In the ensuing gallery, click on
Mock responses
. - In the dedicated textbox, spell out
200 OK, application/json
. This is an indication for the API to return the mock response we established earlier. - Hit
Save
.
Note: Post this, a yellow alert displaying "Mocking is enabled" should surface. This confirms that the API Management responses are not sourced from the backend but are mock-generated.
Validating the mock API
- Go back to the initial API.
- Engage the
Test
tab. - Ensure
Test call API
is active, and then hitSend
to initiate a test call. - If all goes as planned, the HTTP response should mirror the JSON blueprint established earlier.
Conclusion
Azure API Management's mock response capability is a pivotal tool, especially for those inevitable moments when the backend isn't immediately available. With this blog, I hope that you're equipped to maintain seamless development and testing phases, ensuring no disruptions even in the absence of a functional backend.