title: Rewind request body stream
body: <p>I am re-implementing a request logger as Owin Middleware which logs the request url and body of all incoming requests. <strong>I am able to read the body, but if I do the body parameter in my controller is null.</strong></p>
  
  <p>I'm guessing it's null because the stream position is at the end so there is nothing left to read when it tries to deserialize the body. I had a similar issue in a previous version of Web API but was able to set the Stream position back to 0. This particular stream throws a <code>This stream does not support seek operations</code> exception.</p>
  
  <p>In the most recent version of Web API 2.0 I could call <code>Request.HttpContent.ReadAsStringAsync()</code>inside my request logger, and the body would still arrive to the controller in tact.</p>
  
  <p><strong>How can I rewind the stream after reading it?</strong></p>
  
  <p>or </p>
  
  <p><strong>How can I read the request body without consuming it?</strong></p>
  
  <pre><code>public class RequestLoggerMiddleware : OwinMiddleware
  {
      public RequestLoggerMiddleware(OwinMiddleware next)
          : base(next)
      {
      }
  
      public override Task Invoke(IOwinContext context)
      {
          return Task.Run(() =&gt; {
              string body = new StreamReader(context.Request.Body).ReadToEnd();
              // log body
  
              context.Request.Body.Position = 0; // cannot set stream position back to 0
              Console.WriteLine(context.Request.Body.CanSeek); // prints false
              this.Next.Invoke(context);
          });
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class SampleController : ApiController 
  {
      public void Post(ModelClass body)
      {
          // body is now null if the middleware reads it
      }
  }
  </code></pre>