Add API to help set and revert response bodies
See original GitHub issueBackground and Motivation
A very common pattern in ASP.NET Core middleware (and other middleware like components) is to set a temporary response body, execute some user code, then revert to the previous response body when unwinding. The steps go like this:
- Store a reference to the existing body
- Replace the body with the new stream
- Execute user code
- Grab the contents of the body stream and copy it to the previous stream
- Restore the previous body in a finally so exceptional cases are handled
- Don’t forget to dispose any streams created
app.Use(async (context, next) =>
{
var previous = context.Response.Body;
try
{
using var stream = new FileBufferingWriteStream();
context.Response.Body = stream;
await next();
await stream.CopyToAsync(previous);
}
finally
{
context.Response.Body = previous;
}
});
This code today is very manual and we could instead make it a bit more pleasant with some new APIs.
Proposed API
Please provide the specific public API signature diff that you are proposing. For example:
namespace Microsoft.AspNetCore.Http
{
public class HttpResponse
{
+ public ResponseBodyScope UseBody(Stream stream);
}
+ public struct ResponseBodyScope : IDisposable, IAsyncDisposable
+ {
+ public Stream PreviousBody { get; }
+ public Stream CurrentBody { get; }
+ public void Dispose();
+ public ValueTask DisposeAsync();
+ }
}
Usage Examples
app.Use(async (context, next) =>
{
using var scope = context.Response.UseBody(new FileBufferingWriteStream());
await next();
await scope.CurrentBody.CopyToAsync(scope.PreviousBody);
});
Alternative Designs
An alternative might be an explicit push/pop style API but that wouldn’t automagically handle the pop in the failure case.
We may also want to consider a mode that auto-flushes to the previous response body but then failure cases also get weird (did you want to flush the response on failure?)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:11 (9 by maintainers)
Top Results From Across the Web
RESTful API. Should I be returning the object that was ...
I'm designing a RESTful web service using WebApi and was wondering what HTTP responses and response bodies to return when updating / creating ......
Read more >How do I transform the response of my api using set-body ...
I was using these sets of policies in the Demo Conference Api, but im unable to extract only certain properties using the set-body...
Read more >Use a mapping template to override an API's request and ...
Tutorial: Override an API's response status code with the API Gateway console · Under APIs, choose the PetStore API. · In the Resources...
Read more >Modify Response Body
The Modify Response Rule allows you to mock the response body of an HTTP request ... You want to modify API responses but...
Read more >ModResponse - Mock and replay API
ModResponse is a powerful and easy-to-use tool for web developers that simplifies the process of modifying, stubbing, and replaying HTTP responses.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is this issue up for grab?
Any updates on this?