Skip to content

Commit 1dfcb1d

Browse files
Merge pull request #224136 from dlepow/formurl
[APIM] Form URL encoded content in policy expressions
2 parents 9e95905 + 484c330 commit 1dfcb1d

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

articles/api-management/api-management-policy-expressions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: dlepow
77

88
ms.service: api-management
99
ms.topic: article
10-
ms.date: 02/07/2022
10+
ms.date: 01/13/2023
1111
ms.author: danlep
1212
---
1313
# API Management policy expressions
@@ -214,7 +214,7 @@ The `context` variable is implicitly available in every policy [expression](api-
214214
|<a id="ref-context-user"></a>`context.User`|`Email`: `string`<br /><br /> `FirstName`: `string`<br /><br /> `Groups`: `IEnumerable<`[`IGroup`](#ref-igroup)`>`<br /><br /> `Id`: `string`<br /><br /> `Identities`: `IEnumerable<`[`IUserIdentity`](#ref-iuseridentity)`>`<br /><br /> `LastName`: `string`<br /><br /> `Note`: `string`<br /><br /> `RegistrationDate`: `DateTime`|
215215
|<a id="ref-iapi"></a>`IApi`|`Id`: `string`<br /><br /> `Name`: `string`<br /><br /> `Path`: `string`<br /><br /> `Protocols`: `IEnumerable<string>`<br /><br /> `ServiceUrl`: [`IUrl`](#ref-iurl)<br /><br /> `SubscriptionKeyParameterNames`: [`ISubscriptionKeyParameterNames`](#ref-isubscriptionkeyparameternames)|
216216
|<a id="ref-igroup"></a>`IGroup`|`Id`: `string`<br /><br /> `Name`: `string`|
217-
|<a id="ref-imessagebody"></a>`IMessageBody`|`As<T>(preserveContent: bool = false): Where T: string, byte[],JObject, JToken, JArray, XNode, XElement, XDocument`<br /><br /> The `context.Request.Body.As<T>` and `context.Response.Body.As<T>` methods are used to read either a request and response message body in specified type `T`. By default, the method:<br /><ul><li>Uses the original message body stream.</li><li>Renders it unavailable after it returns.</li></ul> <br />To avoid that and have the method operate on a copy of the body stream, set the `preserveContent` parameter to `true`, as in [this example](api-management-transformation-policies.md#SetBody).|
217+
|<a id="ref-imessagebody"></a>`IMessageBody`|`As<T>(bool preserveContent = false): Where T: string, byte[], JObject, JToken, JArray, XNode, XElement, XDocument` <br /><br /> - The `context.Request.Body.As<T>` and `context.Response.Body.As<T>` methods read a request or response message body in specified type `T`. <br/><br/> - Or - <br/><br/>`AsFormUrlEncodedContent(bool preserveContent = false)` <br/></br>- The `context.Request.Body.AsFormUrlEncodedContent()` and `context.Response.Body.AsFormUrlEncodedContent()` methods read URL-encoded form data in a request or response message body and return an `IDictionary<string, IList<string>>` object. The decoded object supports `IDictionary` operations and the following expressions: `ToQueryString()`, `JsonConvert.SerializeObject()`, `ToFormUrlEncodedContent().` <br/><br/> By default, the `As<T>` and `AsFormUrlEncodedContent()` methods:<br /><ul><li>Use the original message body stream.</li><li>Render it unavailable after it returns.</li></ul> <br />To avoid that and have the method operate on a copy of the body stream, set the `preserveContent` parameter to `true`, as shown in examples for the [set-body](set-body-policy.md#examples) policy.|
218218
|<a id="ref-iprivateendpointconnection"></a>`IPrivateEndpointConnection`|`Name`: `string`<br /><br /> `GroupId`: `string`<br /><br /> `MemberName`: `string`<br /><br />For more information, see the [REST API](/rest/api/apimanagement/current-ga/private-endpoint-connection/list-private-link-resources).|
219219
|<a id="ref-iurl"></a>`IUrl`|`Host`: `string`<br /><br /> `Path`: `string`<br /><br /> `Port`: `int`<br /><br /> [`Query`](#ref-iurl-query): `IReadOnlyDictionary<string, string[]>`<br /><br /> `QueryString`: `string`<br /><br /> `Scheme`: `string`|
220220
|<a id="ref-iuseridentity"></a>`IUserIdentity`|`Id`: `string`<br /><br /> `Provider`: `string`|

articles/api-management/set-body-policy.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: dlepow
66

77
ms.service: api-management
88
ms.topic: reference
9-
ms.date: 12/02/2022
9+
ms.date: 01/13/2023
1010
ms.author: danlep
1111
---
1212

@@ -228,14 +228,42 @@ This example shows how to perform content filtering by removing data elements fr
228228

229229
### Transform JSON using a Liquid template
230230
```xml
231+
<set-body template="liquid">
231232
{
232233
"order": {
233234
"id": "{{body.customer.purchase.identifier}}",
234235
"summary": "{{body.customer.purchase.orderShortDesc}}"
235236
}
236237
}
238+
</set-body>
239+
```
240+
241+
### Access the body as URL-encoded form data
242+
The following example uses the `AsFormUrlEncodedContent()` expression to access the request body as URL-encoded form data (content type `application/x-www-form-urlencoded`), and then converts it to JSON. Since we are not reserving the original request body, accessing it later in the pipeline will result in an exception.
243+
244+
```xml
245+
<set-body
246+
@{ 
247+
var inBody = context.Request.Body.AsFormUrlEncodedContent();
248+
return JsonConvert.SerializeObject(inBody); 
249+
250+
</set-body>
237251
```
238252

253+
### Access and return body as URL-encoded form data
254+
The following example uses the `AsFormUrlEncodedContent()` expression to access the request body as URL-encoded form data (content type `application/x-www-form-urlencoded`), adds data to the payload, and returns URL-encoded form data. Since we are not reserving the original request body, accessing it later in the pipeline will result in an exception.
255+
256+
```xml
257+
<set-body
258+
@{ 
259+
var body = context.Request.Body.AsFormUrlEncodedContent();
260+
body["newKey"].Add("newValue");
261+
return body.ToFormUrlEncodedContent(); 
262+
263+
</set-body>
264+
```
265+
266+
239267
## Related policies
240268

241269
* [API Management transformation policies](api-management-transformation-policies.md)

0 commit comments

Comments
 (0)