0

I've created an Owin middleware to log the response coming from other middlewares, but I can see the response in a strange format like

\u001f�\b

I think this is caused as the response headers includes

"Content-Encoding" : "gzip"

and the content type of the response is application/json

this is the code I used to get the response

//Copy a pointer to the original response body stream
        Stream stream = context.Response.Body;
        //Create a new memory stream... 
        using (MemoryStream responseBuffer = new MemoryStream())
        {
            //...and use that for the temporary response body
            context.Response.Body = responseBuffer;



           //Continue down the Middleware pipeline, eventually returning to this class
            await Next.Invoke(context).ConfigureAwait(false);



               //We need to read the response stream from the beginning...
                responseBuffer.Seek(0, SeekOrigin.Begin);
                //...and copy it into a string
                var responseBody = new StreamReader(responseBuffer).ReadToEnd();
                //We need to reset the reader for the response so that the client can read it.
                responseBuffer.Seek(0, SeekOrigin.Begin);
                //Copy the contents of the new memory stream (which contains the response)
                //to the original stream, which is then returned to the client.
                await responseBuffer.CopyToAsync(stream).ConfigureAwait(false);
                context.Response.Body = stream;

0 Answers0