Skip to content

Commit 16d69cf

Browse files
author
gitName
committed
draft complete
1 parent 7112b64 commit 16d69cf

File tree

8 files changed

+60
-78
lines changed

8 files changed

+60
-78
lines changed
162 KB
Loading
171 KB
Loading
Binary file not shown.
Binary file not shown.
113 KB
Loading
Binary file not shown.
7.8 KB
Loading

articles/api-management/transform-api.md

Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ author: dlepow
66
ms.service: azure-api-management
77
ms.custom: mvc, devdivchpfy22
88
ms.topic: tutorial
9-
ms.date: 11/18/2024
9+
ms.date: 11/25/2024
1010
ms.author: danlep
1111
---
1212

1313
# Tutorial: Transform and protect your API
1414

1515
[!INCLUDE [api-management-availability-all-tiers](../../includes/api-management-availability-all-tiers.md)]
1616

17-
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.
1818

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).
2020

2121
> [!NOTE]
2222
> 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.
2323
2424
In this tutorial, you learn how to:
2525

2626
> [!div class="checklist"]
27-
> * Protect an API by adding a rate limit policy (throttling)
2827
> * Transform an API to set a custom response header
28+
> * Protect an API by adding a rate limit policy (throttling)
2929
> * Test the transformations
3030
3131
:::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:
3434

3535
* Learn the [Azure API Management terminology](api-management-terminology.md).
3636
* 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.
3838
* Also, complete the following tutorial: [Import and publish your first API](import-and-publish.md).
3939

4040
[!INCLUDE [api-management-navigate-to-instance.md](../../includes/api-management-navigate-to-instance.md)]
@@ -46,23 +46,47 @@ To see the original response:
4646
1. In your API Management service instance, select **APIs**.
4747
1. Select **Swagger Petstore** from your API list.
4848
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**.
5050

5151
The original API response should look similar to the following response:
5252

5353
:::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.":::
5454

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.
62+
63+
1. Select **Swagger Petstore** > **Design** > **All operations**.
64+
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.":::
5680

5781

5882
## Protect an API by adding rate limit policy (throttling)
5983

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.
6185

6286
> [!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.
6488
65-
1. Select **Petstore API** > **Design** > **All operations**.
89+
1. Select **Swagger Petstore** > **Design** > **All operations**.
6690
1. In the **Inbound processing** section, select the code editor (**</>**) icon.
6791

6892
:::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
79103

80104
1. Modify your **`<rate-limit-by-key />`** code in the **`<inbound>`** element to the following code. Then select **Save**.
81105

82-
```
106+
```xml
83107
<rate-limit-by-key calls="3" renewal-period="15" counter-key="@(context.Subscription.Id)" />
84108
```
85109

86110

87-
## 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.
96-
97-
1. Select **Swagger Petstore** > **Design** > **All operations**.
98-
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-
117111
## Test the transformations
118112

119113
At this point, if you look at the code in the code editor, your policies look like the following code:
120114

121-
```
115+
```xml
122116
<policies>
123-
<inbound>
124-
<rate-limit-by-key calls="3" renewal-period="15" counter-key="@(context.Subscription.Id)" />
125-
<base />
126-
</inbound>
127-
<backend>
128-
<base />
129-
</backend>
130-
<outbound>
131-
<set-header name="X-Powered-By" exists-action="delete" />
132-
<set-header name="X-AspNet-Version" exists-action="delete" />
133-
<redirect-content-urls />
134-
<base />
135-
</outbound>
136-
<on-error>
137-
<base />
138-
</on-error>
139-
</policies>
117+
<inbound>
118+
<rate-limit calls="3" renewal-period="15" counter-key="@(context.Subscription.Id)" />
119+
<base />
120+
</inbound>
121+
<outbound>
122+
<set-header name="Custom" exists-action="override">
123+
<value>"My custom value"</value>
124+
</set-header>
125+
<base />
126+
</outbound>
127+
<on-error>
128+
<base />
129+
</on-error>
130+
</policies>
140131
```
141132

142133
The rest of this section tests policy transformations that you set in this article.
143134

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
150136

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**.
152139

153-
### Test the replaced URL
140+
As you can see, the custom response header is added:
154141

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.":::
157143

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.":::
161144

162145
### Test the rate limit (throttling)
163146

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.
166149

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.
168151

169152
:::image type="content" source="media/transform-api/test-throttling-new.png" alt-text="Screenshot showing Too Many Requests in the response in the portal.":::
170153

@@ -176,9 +159,8 @@ In this tutorial, you learned how to:
176159

177160
> [!div class="checklist"]
178161
>
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)
182164
> * Test the transformations
183165
184166
## Next steps

0 commit comments

Comments
 (0)