Skip to content

Commit 8dd8192

Browse files
authored
Updates
1 parent 6822677 commit 8dd8192

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

aspnetcore/fundamentals/servers/yarp/extensibility-transforms.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ The below example uses simple, inefficient buffering to transform requests. A mo
5252

5353
This sample requires YARP 1.1, see https://github.com/microsoft/reverse-proxy/pull/1569.
5454

55-
#### Example: Modifying an existing request body
5655
```csharp
5756
.AddTransforms(context =>
5857
{
@@ -77,12 +76,10 @@ This sample requires YARP 1.1, see https://github.com/microsoft/reverse-proxy/pu
7776
});
7877
```
7978

80-
### Important limitations
81-
> **Custom transforms can only modify a request body if one is already present** in the incoming request.
82-
> They **cannot add a new body** to a request that originally did not have one (e.g., a POST request with no body or a GET request).
83-
> If you need to add a body where none exists, you must do so in **middleware that runs before YARP**, not in a transform.
79+
Custom transforms can only modify a request body if one is already present. They can't add a new body to a request that doesn't have one (for example, a POST request without a body or a GET request). If you need to add a body for a specific HTTP method and route, you must do so in middleware that runs before YARP, not in a transform.
80+
81+
The following middleware demonstrates how to add a body to a request that doesn't have one:
8482

85-
#### Example: Adding a body to a request that did not originally have one
8683
```csharp
8784
public class AddRequestBodyMiddleware
8885
{
@@ -96,7 +93,8 @@ public class AddRequestBodyMiddleware
9693
public async Task InvokeAsync(HttpContext context)
9794
{
9895
// Only modify specific route and method
99-
if (context.Request.Method == HttpMethods.Post && context.Request.Path == "/my-special-route")
96+
if (context.Request.Method == HttpMethods.Post &&
97+
context.Request.Path == "/special-route")
10098
{
10199
var bodyContent = "key=value";
102100
var bodyBytes = Encoding.UTF8.GetBytes(bodyContent);
@@ -105,8 +103,10 @@ public class AddRequestBodyMiddleware
105103
context.Request.Body = new MemoryStream(bodyBytes);
106104
context.Request.ContentLength = bodyBytes.Length;
107105

108-
// Replace IHttpRequestBodyDetectionFeature so YARP knows a body is present
109-
context.Features.Set<IHttpRequestBodyDetectionFeature>(new CustomBodyDetectionFeature());
106+
// Replace IHttpRequestBodyDetectionFeature so YARP knows
107+
// a body is present
108+
context.Features.Set<IHttpRequestBodyDetectionFeature>(
109+
new CustomBodyDetectionFeature());
110110
}
111111

112112
await _next(context);
@@ -118,13 +118,11 @@ public class AddRequestBodyMiddleware
118118
public bool CanHaveBody => true;
119119
}
120120
}
121-
122121
```
123122

124-
#### Note
123+
> [!NOTE]
125124
> You can use `context.GetRouteModel().Config.RouteId` in middleware to conditionally apply this logic for specific YARP routes.
126125
127-
128126
## Response body transforms
129127

130128
YARP does not provide any built in transforms for modifying the response body. However, the body can be modified by custom transforms.

0 commit comments

Comments
 (0)