You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Tutorial - Mock API responses in API Management - Azure portal | Microsoft Docs
3
-
description: In this tutorial, you use API Management to set a policy on an API. The policy returns a mocked response even if the backend isn't available to send real responses.
3
+
description: Use Azure API Management to set a policy on an API. The policy returns a mock response even if the backend isn't available to send real responses.
Backend APIs are imported into an API Management (APIM) API or created and managed manually. The steps in this tutorial, show you how to:
20
+
Backend APIs are imported into an Azure API Management API or created and managed manually. The steps in this tutorial describe how to:
18
21
19
-
+ Use API Management to create a blank HTTP API
20
-
+Manage an HTTP API manually
21
-
+ Set a policy on an API so it returns a mocked response
22
+
+ Use API Management to create a blank HTTP API.
23
+
+Manually manage an HTTP API.
24
+
+ Set a policy on an API so that it returns a mock response.
22
25
23
-
This method lets developers continue with the implementation and testing of the API Management instance even if the backend isn't available to send real responses.
26
+
This method enables developers to continue with the implementation and testing of the API Management instance even if the backend isn't available to send real responses.
The ability to mock up responses is useful in many scenarios:
30
+
The ability to create mock responses is useful in many scenarios:
28
31
29
-
+ When the API façade is designed first and the backend implementation comes later. Or, the backend is being developed in parallel.
30
-
+ When the backend is temporarily not operational or not able to scale.
32
+
+ When the API façade is designed first and the backend implementation occurs later, or when the backend is being developed in parallel.
33
+
+ When the backend is temporarily not operational or is not able to scale.
31
34
32
35
In this tutorial, you learn how to:
33
36
@@ -37,63 +40,63 @@ In this tutorial, you learn how to:
37
40
> * Enable response mocking
38
41
> * Test the mocked API
39
42
40
-
:::image type="content" source="media/mock-api-responses/mock-api-response-02.png" alt-text="Mocked API response":::
43
+
:::image type="content" source="media/mock-api-responses/mock-api-response-02.png" alt-text="Screenshot that shows the APIs page in the Azure portal." lightbox="media/mock-api-responses/mock-api-response-02.png":::
41
44
42
45
## Prerequisites
43
46
44
-
+ Learn the [Azure API Management terminology](api-management-terminology.md).
45
-
+ Understand the [concept of policies in Azure API Management](api-management-howto-policies.md).
46
-
+ Complete the following quickstart:[Create an Azure API Management instance](get-started-create-service-instance.md).
:::image type="content" source="media/mock-api-responses/http-api.png" alt-text="Define a HTTP API":::
58
+
:::image type="content" source="media/mock-api-responses/http-api.png" alt-text="Screenshot that shows the first steps for defining an API." lightbox="media/mock-api-responses/http-api.png":::
56
59
57
60
1. In the **Create an HTTP API** window, select **Full**.
58
-
1.Enter *Test API* for **Display name**.
59
-
1.Select**Unlimited** for **Products**.
60
-
1.Ensure that **Managed** is selected for **Gateways**.
61
+
1.In **Display name**, enter *Test API*.
62
+
1.In**Products**, select *Unlimited*, if that value is available. This value is available only in some tiers. You can leave the value blank for this tutorial, but you need to associate the API with a product to publish it. For more information, see [Import and publish your first API](import-and-publish.md#import-and-publish-a-backend-api).
63
+
1.In **Gateways**, select **Managed**if this option is available. (This option is avaiable only in certain service tiers.)
61
64
1. Select **Create**.
62
65
63
-
:::image type="content" source="media/mock-api-responses/create-http-api.png" alt-text="Create an HTTP API":::
66
+
:::image type="content" source="media/mock-api-responses/create-http-api.png" alt-text="Screenshot that shows the Create an HTTP API window." lightbox="media/mock-api-responses/create-http-api.png":::
64
67
65
68
## Add an operation to the test API
66
69
67
-
An API exposes one or more operations. In this section, you'll add an operation to the HTTP API you created. Calling the operation after completing the steps in this section triggers an error. After you complete the steps in the [Enable response mocking](#enable-response-mocking) section, you'll get no errors.
70
+
An API exposes one or more operations. In this section, you add an operation to the HTTP API you created. Calling the operation after completing the steps in this section triggers an error. After you complete the steps in the [Enable response mocking](#enable-response-mocking) section, you won't get an error.
68
71
69
72
### [Portal](#tab/azure-portal)
70
73
71
-
1. Select the API you created in the previous step.
74
+
1. Select the API that you created in the previous step.
72
75
1. Select **+ Add Operation**.
73
-
1. In the **Frontend** window, enter the following values.
76
+
1. In the **Frontend** window, enter the following values:
|**Display name**|*Test call*| The name that is displayed in the [developer portal](api-management-howto-developer-portal.md). |
80
-
|**URL** (HTTP verb) | GET | Select one of the predefined HTTP verbs. |
81
-
|**URL**|*/test*| A URL path for the API. |
82
-
|**Description**|| Optional description of the operation, used to provide documentation in the developer portal to the developers using this API. |
85
+
:::image type="content" source="media/mock-api-responses/frontend-window.png" alt-text="Screenshot that shows the Frontend window." lightbox="media/mock-api-responses/frontend-window.png":::
83
86
84
-
1. Select the **Responses** tab, located under the URL, Display name, and Description fields. Enter settings on this tab to define response status codes, content types, examples, and schemas.
85
-
1. Select **+ Add response**, and select **200 OK** from the list.
87
+
1. Select the **Responses** tab, which is located under the **URL**, **Display name**, and **Description** boxes. You'll enter values on this tab to define response status codes, content types, examples, and schemas.
88
+
1. Select **+ Add response**, and then select **200 OK** from the list.
86
89
87
-
:::image type="content" source="media/mock-api-responses/add-response.png" alt-text="Add response to the API operation":::
90
+
:::image type="content" source="media/mock-api-responses/add-response.png" alt-text="Screenshot that shows the Responses tab." lightbox="media/mock-api-responses/add-response.png":::
88
91
89
-
1.Under the **Representations**heading on the right, select **+ Add representation**.
90
-
1. Enter *application/json* into the search box and select the **application/json** content type.
91
-
1. In the **Sample**text box, enter `{ "sampleField" : "test" }`.
92
+
1.In the **Representations**section, select **+ Add representation**.
93
+
1. Enter *application/json* into the search box and then select the **application/json** content type.
94
+
1. In the **Sample** box, enter `{ "sampleField" : "test" }`.
92
95
1. Select **Save**.
93
96
94
-
:::image type="content" source="media/mock-api-responses/add-representation.png" alt-text="Add representation to the API operation":::
97
+
:::image type="content" source="media/mock-api-responses/add-representation.png" alt-text="Screenshot that shows the Representations section." lightbox="media/mock-api-responses/add-representation.png":::
95
98
96
-
Although not required for this example, you can configure more settings for an API operation on other tabs, including:
99
+
Although it's not required for this example, you can configure more settings for an API operation on other tabs, as described in the following table:
97
100
98
101
|Tab |Description |
99
102
|---------|---------|
@@ -109,75 +112,64 @@ To begin using Azure CLI:
109
112
To add an operation to your test API, run the [az apim api operation create](/cli/azure/apim/api/operation#az-apim-api-operation-create) command:
110
113
111
114
```azurecli
112
-
az apim api operation create --resource-group apim-hello-word-resource-group \
115
+
az apim api operation create --resource-group <resource-group> \
113
116
--display-name "Test call" --api-id test-api --method GET \
To remove an operation, use the [az apim api operation delete](/cli/azure/apim/api/operation#az-apim-api-operation-delete) command. Get the operation ID from the previous command.
127
+
Keep this operation for use in the rest of this article. If you want to remove an operation, you can use the [az apim api operation delete](/cli/azure/apim/api/operation#az-apim-api-operation-delete) command. Get the operation ID from the previous command.
125
128
126
129
```azurecli
127
-
az apim api operation delete --resource-group apim-hello-word-resource-group \
:::image type="content" source="media/mock-api-responses/add-policy.png" alt-text="Screenshot that shows the first steps for enabling response mocking." lightbox="media/mock-api-responses/add-policy.png" :::
144
145
145
-
1. Select **Mock responses** from the gallery.
146
+
1. Select the **Mock responses**tile from the gallery:
:::image type="content" source="media/mock-api-responses/mock-responses-policy-tile.png" alt-text="Screenshot that shows the Mock responses tile." border="false":::
148
149
149
-
1.In the**API Management response**textbox, type **200 OK, application/json**. This selection indicates that your API should return the response sample you defined in the previous section.
150
+
1.Ensure that**200 OK, application/json**appears in the **API Management response** box. This selection indicates that your API should return the response sample that you defined in the previous section.
:::image type="content" source="media/mock-api-responses/set-mocking-response.png" alt-text="Screenshot that shows the API Management response selection." lightbox="media/mock-api-responses/set-mocking-response.png":::
152
153
153
154
1. Select **Save**.
154
155
155
156
> [!TIP]
156
-
> A yellow bar with the text **Mocking is enabled**displays. This indicates that the responses returned from API Management are mocked by the [mocking policy](mock-response-policy.md) and aren't produced by the backend.
157
+
> A yellow bar displaying the text **Mocking is enabled**appears. This message indicates that the responses returned from API Management are mocked by the [mocking policy](mock-response-policy.md) and aren't produced by the backend.
157
158
158
159
## Test the mocked API
159
160
160
161
1. Select the API you created in [Create a test API](#create-a-test-api).
161
-
1. Select the **Test** tab.
162
-
1. Ensure that the **Test call** API is selected, and then select **Send** to make a test call.
163
-
164
-
:::image type="content" source="media/mock-api-responses/test-mock-api.png" alt-text="Test the mocked API":::
165
-
166
-
1. The **HTTP response** displays the JSON provided as a sample in the first section of the tutorial.
162
+
1. On the **Test** tab, ensure that the **Test call** API is selected, and then select **Send** to make a test call:
:::image type="content" source="media/mock-api-responses/test-mock-api.png" alt-text="Screenshot that shows the steps for testing the mocked API." lightbox="media/mock-api-responses/test-mock-api.png":::
169
165
170
-
## Next steps
166
+
1. The **HTTP response** displays the JSON provided as a sample in the first section of the tutorial:
171
167
172
-
In this tutorial, you learned how to:
168
+
:::image type="content" source="media/mock-api-responses/http-response.png" alt-text="Screenshot that shows the mock HTTP response." lightbox="media/mock-api-responses/http-response.png":::
173
169
174
-
> [!div class="checklist"]
175
-
> * Create a test API
176
-
> * Add an operation to the test API
177
-
> * Enable response mocking
178
-
> * Test the mocked API
170
+
## Next step
179
171
180
-
Advance to the next tutorial:
172
+
Go to the next tutorial:
181
173
182
174
> [!div class="nextstepaction"]
183
175
> [Transform and protect a published API](transform-api.md)
0 commit comments