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
In this tutorial, you learn about configuring [policies](api-management-howto-policies.md) to protect or transform your API. For example, protect your backend API by configuring a rate limit policy, so that the API isn't overused by developers. You might want to transform your API so it doesn't reveal private backend info or other potentially sensitive information, or to set a custom header.
17
+
In this tutorial, you learn about configuring [policies](api-management-howto-policies.md) to protect or transform your API. Policies are a collection of statements that are run sequentially on the request or response of an API that modify the API's behavior.
18
18
19
-
For more policy options, see [API Management policies](api-management-policies.md).
19
+
For example, you might want to set a custom response header. Or, protect your backend API by configuring a rate limit policy, so that the API isn't overused by developers. These examples are just an introduction to to API Management policies. For more policy options, see [API Management policies](api-management-policies.md).
20
20
21
21
> [!NOTE]
22
22
> By default, API Management configures a global [`forward-request`](forward-request-policy.md) policy. The `forward-request` policy is needed for the gateway to complete a request to a backend service.
23
23
24
24
In this tutorial, you learn how to:
25
25
26
26
> [!div class="checklist"]
27
-
> * Protect an API by adding a rate limit policy (throttling)
28
27
> * Transform an API to set a custom response header
28
+
> * Protect an API by adding a rate limit policy (throttling)
29
29
> * Test the transformations
30
30
31
31
:::image type="content" source="media/transform-api/api-management-console-new.png" lightbox="media/transform-api/api-management-console-new.png" alt-text="Screenshot of API Management policies in the portal.":::
@@ -34,7 +34,7 @@ In this tutorial, you learn how to:
34
34
35
35
* Learn the [Azure API Management terminology](api-management-terminology.md).
36
36
* Understand the [concept of policies in Azure API Management](api-management-howto-policies.md).
37
-
* Complete the following quickstart: [Create an Azure API Management instance](get-started-create-service-instance.md). For this tutorial, we recommend the Developer tier or the Basic v2 tier. The Consumption tier doesn't support all policies used in this tutorial.
37
+
* Complete the following quickstart: [Create an Azure API Management instance](get-started-create-service-instance.md). For this tutorial, we recommend that you use one of the classic or v2 tiers, for example, the Developer tier or the Basic v2 tier. The Consumption tier doesn't support all policies used in this tutorial.
38
38
* Also, complete the following tutorial: [Import and publish your first API](import-and-publish.md).
1. In your API Management service instance, select **APIs**.
47
47
1. Select **Swagger Petstore** from your API list.
48
48
1. Select the **Test** tab, on the top of the screen.
49
-
1. Select the **GetSpeakers** operation, and then select **Send**.
49
+
1. Select the **GET Finds pets by status** operation, and optionally select a different value of the *status***Query parameter**. Select**Send**.
50
50
51
51
The original API response should look similar to the following response:
52
52
53
53
:::image type="content" source="media/transform-api/test-original-response-new.png" lightbox="media/transform-api/test-original-response-new.png" alt-text="Screenshot of the original API response in the portal.":::
54
54
55
-
As you can see, the response includes the **X-AspNet-Version** and **X-Powered-By** headers.
55
+
## Transform an API to add a custom response header
56
+
57
+
API Management includes several transformation policies that you can use to modify request or response payloads, headers, or status codes. In this example, you set a custom response header in the API response.
58
+
59
+
### Set the transformation policy
60
+
61
+
This section shows you how to configure a custom response header using the `set-header` policy. Here you use a form-based policy editor that simplifies the policy configuration.
1. In the **Outbound processing** section, select **+ Add policy**.
65
+
66
+
:::image type="content" source="media/transform-api/outbound-policy-small.png" alt-text="Screenshot of navigating to outbound policy in the portal." lightbox="media/transform-api/outbound-policy.png":::
67
+
68
+
1. In the **Add outbound policy** window, select **Set headers**.
69
+
70
+
:::image type="content" source="media/transform-api/set-http-header.png" alt-text="Screenshot of configuring the Set headers policy in the portal.":::
71
+
72
+
1. To configure the Set headers policy, do the following:
73
+
1. Under **Name**, enter **Custom**.
74
+
1. Under **Value**, select **+ Add value**. Enter *"My custom value"*.
75
+
1. Select **Save**.
76
+
77
+
1. After configuration, a **set-header** policy element appears in the **Outbound processing** section.
78
+
79
+
:::image type="content" source="media/transform-api/set-policy.png" alt-text="Screenshot of the Set headers outbound policies in the portal.":::
56
80
57
81
58
82
## Protect an API by adding rate limit policy (throttling)
59
83
60
-
This section shows how to add protection to your backend API by configuring rate limits, so that the API isn't overused by developers. In this example, the limit is set to three calls per 15 seconds. After 15 seconds, a developer can retry calling an API.
84
+
This section shows how to add protection to your backend API by configuring rate limits, so that the API isn't overused by developers. This example shows how to configure the `rate-limit-by-key` policy using the code editor. In this example, the limit is set to three calls per 15 seconds. After 15 seconds, a developer can retry calling tge API.
61
85
62
86
> [!NOTE]
63
-
> This example shows how to configure the `rate-limit-by-key` policy using the code editor. This policy isn't supported in the Consumption tier.
87
+
> This policy isn't supported in the Consumption tier.
1. In the **Inbound processing** section, select the code editor (**</>**) icon.
67
91
68
92
:::image type="content" source="media/transform-api/inbound-policy-code.png" lightbox="media/transform-api/inbound-policy-code.png" alt-text="Screenshot of navigating to inbound policy code editor in the portal.":::
@@ -79,92 +103,51 @@ This section shows how to add protection to your backend API by configuring rate
79
103
80
104
1. Modify your **`<rate-limit-by-key />`** code in the **`<inbound>`** element to the following code. Then select **Save**.
## Transform an API to add a custom response header
88
-
89
-
API Management includes several transformation policies that you can use to modify request or response payloads, headers, or status codes. As an example, this section shows you how to configure a custom response header using the `set-header` policy.
90
-
91
-
92
-
93
-
### Set the transformation policy
94
-
95
-
This example shows how to use the form-based policy editor, which helps you configure many policies without having to edit the policy XML statements directly.
1. In the **Outbound processing** section, select **+ Add policy**.
99
-
100
-
:::image type="content" source="media/transform-api/outbound-policy-small.png" alt-text="Screenshot of navigating to outbound policy in the portal." lightbox="media/transform-api/outbound-policy.png":::
101
-
102
-
1. In the **Add outbound policy** window, select **Set headers**.
103
-
104
-
:::image type="content" source="media/transform-api/set-http-header.png" alt-text="Screenshot of configuring the Set headers policy in the portal.":::
105
-
106
-
1. To configure the Set headers policy, do the following:
107
-
1. Under **Name**, enter **Custom header**.
108
-
1. Under **Value**, select **+ Add value**. Enter *My custom value*.
109
-
1. Select **Save**.
110
-
111
-
1. After configuration, a **set-header** policy element appears in the **Outbound processing** section.
112
-
113
-
:::image type="content" source="media/transform-api/set-policy.png" alt-text="Screenshot of the Set headers outbound policies in the portal.":::
114
-
115
-
116
-
117
111
## Test the transformations
118
112
119
113
At this point, if you look at the code in the code editor, your policies look like the following code:
The rest of this section tests policy transformations that you set in this article.
143
134
144
-
### Test the stripped response headers
145
-
146
-
1. Select **Demo Conference API** > **Test**.
147
-
1. Select the **GetSpeakers** operation and select **Send**.
148
-
149
-
As you can see, the **X-AspNet-Version** and **X-Powered-By** headers were removed:
135
+
### Test the custom response header
150
136
151
-
:::image type="content" source="media/transform-api/stripped-response-headers.png" alt-text="Screenshot showing stripped response headers in the portal.":::
137
+
1. Select **Swagger Petstore** > **Test**.
138
+
1. SSelect the **GET Finds pets by status** operation, and optionally select a different value of the *status***Query parameter**. Select **Send**.
152
139
153
-
### Test the replaced URL
140
+
As you can see, the custom response header is added:
154
141
155
-
1. Select **Demo Conference API** > **Test**.
156
-
1. Select the **GetSpeakers** operation and select **Send**.
142
+
:::image type="content" source="media/transform-api/custom-response-header.png" alt-text="Screenshot showing custom response header in the portal.":::
157
143
158
-
As you can see, the URLs are replaced.
159
-
160
-
:::image type="content" source="media/transform-api/test-replaced-url.png" alt-text="Screenshot showing replaced URLs in the portal.":::
161
144
162
145
### Test the rate limit (throttling)
163
146
164
-
1. Select **Demo Conference API** > **Test**.
165
-
1. Select the **GetSpeakers** operation. Select **Send** four times in a row.
147
+
1. Select **Swagger Petstore** > **Test**.
148
+
1. Select the **GET Finds Pets by Status** operation. Select **Send**several times in a row.
166
149
167
-
After sending the request four times, you get the **429 Too Many Requests** response.
150
+
After sending too many requests in the configured period, you get the **429 Too Many Requests** response.
168
151
169
152
:::image type="content" source="media/transform-api/test-throttling-new.png" alt-text="Screenshot showing Too Many Requests in the response in the portal.":::
170
153
@@ -176,9 +159,8 @@ In this tutorial, you learned how to:
176
159
177
160
> [!div class="checklist"]
178
161
>
179
-
> * Transform an API to strip response headers
180
-
> * Replace original URLs in the body of the API response with API Management gateway URLs
181
-
> * Protect an API by adding rate limit policy (throttling)
162
+
> * Transform an API to set a custom response header
163
+
> * Protect an API by adding a rate limit policy (throttling)
0 commit comments