title: How can I read http request body in netcore 3 more than once?
body: <p>I have a netcore 3 API application that logs the incoming request and then passes it on to the controller action.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>public RequestLoggingHandler(IHttpContextAccessor httpContextAccessor)
  {
      _httpContextAccessor = httpContextAccessor;
  }
  
  
  protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RequestLoggingRequirement requirement)
  {
      try
      {
          var httpContext = _httpContextAccessor.HttpContext;
          var request = httpContext.Request;
  
          _repository = (ICleanupOrderRepository)httpContext.RequestServices.GetService(typeof(ICleanupOrderRepository));
          _cache = (IMemoryCache)httpContext.RequestServices.GetService(typeof(IMemoryCache));
  
          httpContext.Items["requestId"] = SaveRequest(request);
  
          context.Succeed(requirement);
  
          return Task.CompletedTask;
      }
      catch (Exception ex)
      {
  
          throw ex;
      }
  }
  
  private int SaveRequest(HttpRequest request)
  {
      try
      {
          // Allows using several time the stream in ASP.Net Core
          var buffer = new byte[Convert.ToInt32(request.ContentLength)];
         request.Body.ReadAsync(buffer, 0, buffer.Length);
  
          var requestContent = Encoding.UTF8.GetString(buffer);
          var requestId = _repository.SaveRawHandlerRequest($"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {requestContent}");
  
          return requestId;
      }
      catch (Exception ex)
      {
  
          throw ex;
      }
  }
  </code></pre>
  
  <p>However when this request is passed on to the Controller the request body is null. </p>
  
  <p>Previously in Core2.x you could do</p>
  
  <pre><code>request.EnableRewind();
  </code></pre>
  
  <p>My understanding is this is now replaced with  </p>
  
  <pre><code>httpContext.Request.EnableBuffering();
  </code></pre>
  
  <p>However, even with  </p>
  
  <pre><code>httpContext.Request.EnableBuffering();
  </code></pre>
  
  <p>the request body is still null once the request body is read. </p>
  
  <p>How can I get around this ?</p>