Rewind request body stream

Just found one solution. Replacing the original stream with a new stream containing the data.

    public override async Task Invoke(IOwinContext context)
    {
        return Task.Run(() => {
            string body = new StreamReader(context.Request.Body).ReadToEnd();
            // log body

            byte[] requestData = Encoding.UTF8.GetBytes(body);
            context.Request.Body = new MemoryStream(requestData);
            await this.Next.Invoke(context);
        });
    }

If you are dealing with larger amounts of data, I’m sure a FileStream would also work as the replacement.

Leave a Comment