-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTransformer.cs
More file actions
29 lines (26 loc) · 1.03 KB
/
CustomTransformer.cs
File metadata and controls
29 lines (26 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using Yarp.ReverseProxy.Forwarder;
public class CustomTransformer : HttpTransformer
{
public virtual async ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest)
{
var requestBody = await httpContext.Request.ReadBodyAsync();
Console.WriteLine($"Request to /test: {requestBody}");
}
public virtual async ValueTask TransformResponseAsync(HttpContext httpContext, HttpResponseMessage proxyResponse)
{
var responseBody = await proxyResponse.Content.ReadAsStringAsync();
Console.WriteLine($"Response from /test: {responseBody}");
}
}
public static class HttpExtensions
{
public static async Task<string> ReadBodyAsync(this HttpRequest request)
{
request.EnableBuffering();
var buffer = new byte[Convert.ToInt32(request.ContentLength)];
await request.Body.ReadAsync(buffer, 0, buffer.Length);
var bodyAsText = System.Text.Encoding.UTF8.GetString(buffer);
request.Body.Position = 0;
return bodyAsText;
}
}