1

I'm using .netCore 3.1 to create a RESTful API.

Here I'm trying to modify the response body to filter out some values based on a corporate use case that I have.

My problem is that at first, I figured that the CanRead value of context.HttpContext.Response.Body is false, thus it is unreadable, so I searched around and found this question and its answers.

which

basically converts a stream that can't seek to one that can

so I applied the answer with a little modification to fit my use case :

        Stream originalBody = context.HttpContext.Response.Body;

        try
        {
            using (var memStream = new MemoryStream())
            {
                context.HttpContext.Response.Body = memStream;


                memStream.Position = 0;
                string responseBody = new StreamReader(memStream).ReadToEnd();

                memStream.Position = 0;
                memStream.CopyTo(originalBody);
                string response_body = new StreamReader(originalBody).ReadToEnd();
                PagedResponse<List<UserPhoneNumber>> deserialized_body;
                deserialized_body = JsonConvert.DeserializeObject<PagedResponse<List<UserPhoneNumber>>>(response_body);
                // rest of code logic
            }

        }
        finally
        {
            context.HttpContext.Response.Body = originalBody;
        }

But when debugging, I found out that memStream.Length is always 0, and therefore the originalBody value is an empty string : "" . Even though after this is executed , the response is returned successfully, (thanks to the finally block).

I can't seem to understand why this is happening, is this an outdated method? what am I doing wrong? Thank you in advance.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Ataa Aub
  • 127
  • 15
  • Where is your code living, in a controller or in middleware? The answers in that question you linked to are all referring to middleware, which has the ability to directly access HTTP request and response streams that controllers do not. – Ian Kemp Dec 10 '20 at 13:12
  • it's in a IResultFilter, which has a `ResultExecutingContext` parameter named `context` and I'm accessing the http response like this : `context.HttpContext.Response.Body` – Ataa Aub Dec 10 '20 at 13:37
  • An `IResultFilter` **is not middleware**. You are trying to put a square peg in a round hole, and you are surprised that it does not fit. – Ian Kemp Dec 10 '20 at 13:39
  • Yeah I know that it's not a middleware, and I'm not following the answer word by word, I just wanted to understand the way that the http response is read. And isn't the result filter supposed to be executed after the result is set? are you suggesting that I use a middleware instead of a ResultFilter? – Ataa Aub Dec 10 '20 at 13:54

1 Answers1

0

using is closing the stream try

string body= new StreamReader(Request.Body).ReadToEnd();
fuzzybear
  • 2,325
  • 3
  • 23
  • 45
  • Thanks for the answer @saj !. but I'm getting this exception : `System.ArgumentException: 'Stream was not readable.'` at the line you suggested. and that's why I originally tried the approach. – Ataa Aub Dec 10 '20 at 10:19