Skip to content

Commit 37a9872

Browse files
committed
[APIM][UUF] Added validate-content example
1 parent f41067c commit 37a9872

1 file changed

Lines changed: 70 additions & 2 deletions

File tree

articles/api-management/validate-content-policy.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ author: dlepow
66

77
ms.service: azure-api-management
88
ms.topic: reference
9-
ms.date: 08/30/2024
9+
ms.date: 02/23/2026
1010
ms.author: danlep
1111
---
1212

1313
# Validate content
1414

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

17-
The `validate-content` policy validates the size or content of a request or response body against one or more [supported schemas](#schemas-for-content-validation).
17+
The `validate-content` policy validates the size or content (or both) of a request or response body against one or more [supported schemas](#schemas-for-content-validation).
1818

1919
The following table shows the schema formats and request or response content types that the policy supports. Content type values are case insensitive.
2020

@@ -161,6 +161,74 @@ In the following example, API Management interprets any request as a request wit
161161
</validate-content>
162162
```
163163

164+
### Complete policy example with content validation
165+
166+
The following example shows a complete policy document for a customer order API that uses `validate-content` to validate incoming requests and outgoing responses. The policy validates that customer order payloads conform to the `customer-order-schema` (added to API Management) before forwarding them to the backend, and also validates that the backend's order confirmation matches the expected schema, but only detects issues rather than blocking them.
167+
168+
169+
```xml
170+
<policies>
171+
<inbound>
172+
<base />
173+
<!-- Authenticate the request -->
174+
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized">
175+
<openid-config url="https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration" />
176+
<audiences>
177+
<audience>api://customer-orders</audience>
178+
</audiences>
179+
</validate-jwt>
180+
181+
<!-- Rate limit per subscription -->
182+
<rate-limit-by-key calls="100" renewal-period="60" counter-key="@(context.Subscription.Id)" />
183+
184+
<!-- Validate incoming order request -->
185+
<validate-content unspecified-content-type-action="prevent" max-size="524288" size-exceeded-action="prevent" errors-variable-name="requestValidationErrors">
186+
<content type="application/json" validate-as="json" schema-id="customer-order-schema" action="prevent" allow-additional-properties="false" />
187+
</validate-content>
188+
189+
<!-- Set backend URL -->
190+
<set-backend-service base-url="https://orders-backend.contoso.com/api" />
191+
</inbound>
192+
<backend>
193+
<base />
194+
</backend>
195+
<outbound>
196+
<base />
197+
198+
<!-- Validate backend response -->
199+
<validate-content unspecified-content-type-action="detect" max-size="1048576" size-exceeded-action="detect" errors-variable-name="responseValidationErrors">
200+
<content type="application/json" validate-as="json" schema-id="order-confirmation-schema" action="detect" />
201+
</validate-content>
202+
203+
<!-- Add custom header to indicate validation passed -->
204+
<set-header name="X-Content-Validated" exists-action="override">
205+
<value>true</value>
206+
</set-header>
207+
</outbound>
208+
<on-error>
209+
<base />
210+
<!-- Return validation errors in a structured format -->
211+
<choose>
212+
<when condition="@(context.Variables.ContainsKey("requestValidationErrors"))">
213+
<return-response>
214+
<set-status code="400" reason="Bad Request" />
215+
<set-header name="Content-Type" exists-action="override">
216+
<value>application/json</value>
217+
</set-header>
218+
<set-body>@{
219+
var errors = (IEnumerable<object>)context.Variables["requestValidationErrors"];
220+
return JsonConvert.SerializeObject(new {
221+
error = "Request validation failed",
222+
details = errors
223+
});
224+
}</set-body>
225+
</return-response>
226+
</when>
227+
</choose>
228+
</on-error>
229+
</policies>
230+
```
231+
164232
[!INCLUDE [api-management-validation-policy-error-reference](../../includes/api-management-validation-policy-error-reference.md)]
165233

166234
## Related policies

0 commit comments

Comments
 (0)