Skip to content

How to transform response body? #520

Answered by Tratcher
prabh-62 asked this question in Q&A
Discussion options

You must be logged in to vote

context.Features.Set<IHttpResponseBodyFeature>(new StreamResponseBodyFeature(memoryResponse));

This line is redundant with context.Response.Body = memoryResponse;, you can remove it.

await originalResponse.CopyToAsync(memoryResponse).ConfigureAwait(false);

This is backwards, it should be memoryResponse.Position = 0; await memoryResponse.CopyToAsync(originalResponse).ConfigureAwait(false);

context.Response.Headers.ContentLength = replaced.Length;

You have to set the ContentLength before any writes to the originalResponse. Setting it after causes an exception and the response gets aborted.

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@prabh-62
Comment options

Comment options

You must be logged in to vote
6 replies
@davidfowl
Comment options

@prabh-62
Comment options

@Tratcher
Comment options

@prabh-62
Comment options

@Tratcher
Comment options

Answer selected by prabh-62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants