Skip to content

Instantly share code, notes, and snippets.

@uzbekdev1
Created October 6, 2025 09:37
Show Gist options
  • Save uzbekdev1/8a0699f4f65f303f7961a6d03e26ea7f to your computer and use it in GitHub Desktop.
Save uzbekdev1/8a0699f4f65f303f7961a6d03e26ea7f to your computer and use it in GitHub Desktop.
request buffering
public class GlobalMiddleware
{
private readonly RequestDelegate _next;
private string _requestBody;
public GlobalMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Method == "PUT" || context.Request.Method == "POST"|| context.Request.Method == "PATCH")
{
context.Request.EnableBuffering();
_requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync();
context.Request.Body.Position = 0;
}
await _next(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment