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
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/servers/yarp/extensibility-transforms.md
+10-12Lines changed: 10 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,6 @@ The below example uses simple, inefficient buffering to transform requests. A mo
52
52
53
53
This sample requires YARP 1.1, see https://github.com/microsoft/reverse-proxy/pull/1569.
54
54
55
-
#### Example: Modifying an existing request body
56
55
```csharp
57
56
.AddTransforms(context=>
58
57
{
@@ -77,12 +76,10 @@ This sample requires YARP 1.1, see https://github.com/microsoft/reverse-proxy/pu
77
76
});
78
77
```
79
78
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:
84
82
85
-
#### Example: Adding a body to a request that did not originally have one
86
83
```csharp
87
84
publicclassAddRequestBodyMiddleware
88
85
{
@@ -96,7 +93,8 @@ public class AddRequestBodyMiddleware
96
93
publicasyncTaskInvokeAsync(HttpContextcontext)
97
94
{
98
95
// 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")
100
98
{
101
99
varbodyContent="key=value";
102
100
varbodyBytes=Encoding.UTF8.GetBytes(bodyContent);
@@ -105,8 +103,10 @@ public class AddRequestBodyMiddleware
105
103
context.Request.Body=newMemoryStream(bodyBytes);
106
104
context.Request.ContentLength=bodyBytes.Length;
107
105
108
-
// Replace IHttpRequestBodyDetectionFeature so YARP knows a body is present
0 commit comments