title: HttpContext.Response.Body.Position = 0 - "Specified method is not supported" error
body: <p>I've got some logging middleware I've setup that grabs and logs information utilizing HttpContext.</p>
  
  <p>I need to set the position of the HttpResponse.Body to 0 in order to read the whole stream, however, no matter what I try, it throws "Specified method is not supported" and fails.</p>
  
  <p>This is very weird to me because position is built right into HttpResponse.Body and I've used it before successfully.</p>
  
  <p>I also tried to use HttpResponse.Body.Seek with the same result. </p>
  
  <p>At this point I'm stuck, any help would be appreciated.</p>
  
  <p>UPDATE: I was able to get the response.body position to change once I moved it into a new memory stream, however, now it returns an empty body back.</p>
  
  <pre><code>public async Task Invoke(HttpContext context)
          {
              //Retrieve request &amp; response
              var request = context.Request;
              var response = context.Response;
  
              if (request.Path != "/")
              {
                  var reqBody = request.Body;
                  var resBody = response.Body;
                  string path = request.Path;
                  string method = request.Method;
                  string queryString = HttpUtility.UrlDecode(request.QueryString.ToString());
                  int statusCode = context.Response.StatusCode;
  
                  var buffer = new byte[Convert.ToInt32(request.ContentLength)];
                  await request.Body.ReadAsync(buffer, 0, buffer.Length);
                  var reqBodyText = Encoding.UTF8.GetString(buffer);
                  request.Body = reqBody;
  
                  var responseBodyStream = new MemoryStream();
                  context.Response.Body = responseBodyStream;
  
                  await _next(context);
  
                  responseBodyStream.Seek(0, SeekOrigin.Begin);
                  var resBodyText = new StreamReader(responseBodyStream).ReadToEnd();
                  responseBodyStream.Seek(0, SeekOrigin.Begin);
                  await responseBodyStream.CopyToAsync(context.Response.Body);
  
                  ...
              }
          }
  </code></pre>