Description
Describe the new topic
- Explain why this topic is needed.
It's not uncommon for apps to buffer the request body in order to do secondary processing, logging, etc..
- Suggest a location in the Table of Contents.
Under Advanced?
- Write an abstract. In one short paragraph, describe what this topic will cover.
Request bodies are streamed by default, meaning only one component gets a chance to read it. If that component wants to preserve the original data for later components in the pipeline it needs buffer the full request body. The HttpRequest.EnableBuffering API can help with this.
- Create an outline for the new topic. We'll help review the outline and approve it before anyone writes a topic.
Why buffer - For replay
When to avoid buffering - Very large content,
Where does the buffer go - X amount in memory, and then to a temp file on disk that's deleted at the end of the request.
Affects on request body size limits - The server enforces a request body size limit that defaults to ~30mb. This can be overridden per request using the IHttpMaxRequestBodySizeFeature. MVC controllers may use an attribute to override this, but if you buffer the body too soon then that may not have happened yet.
An example middleware
context.Request.EnableBuffering();
await context.Request.Body.DrainAsync(); // Or your own processing that reads the body.
context.Request.Body.Position = 0;
await next();
See comments starting at dotnet/aspnetcore#7644 (comment) and continuing at least through 9/24/20.