title: Replacing Request/Response Body in asp.net core middleware
body: <p>I want to replace the request/response body in my middleware. Suppose if client sends <em>Hello</em>, I want to change it to <em>Hola</em> and similarly with response. I found the code that works but not to my requirement. My question is why this works and the other do not. It is actually the same code.</p>
  <p><strong>Working Code</strong></p>
  <pre><code>public async Task InvokeAsync(HttpContext context, RequestDelegate next)
         {
              var request = context.Request;
              var stream = request.Body;// currently holds the original stream                    
              var originalReader = new StreamReader(stream);
              var originalContent = await originalReader.ReadToEndAsync();
              var notModified = true;
              try
              {
  
                  if (originalContent != null)
                  {
                      //Master is some model name
                      var modifiedData = new Master() { Id = &quot;Changed in Middleware&quot;, Email = &quot;Changed&quot; };
                      var json = JsonConvert.SerializeObject(modifiedData);
                      //json variable is just a string here
                      //modified stream
                      var requestData = Encoding.UTF8.GetBytes(json);
                      stream = new MemoryStream(requestData);
                      notModified = false;
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.Message);
              }
              if (notModified)
              {
                  //putting original data
                  var requestData = Encoding.UTF8.GetBytes(originalContent);
                  stream = new MemoryStream(requestData);
              }
  
              request.Body = stream;
              
              await next(context);
          }
  </code></pre>
  <p><strong>Not working Code But My Requirement</strong></p>
  <pre><code>public async Task InvokeAsync(HttpContext context, RequestDelegate next)
         {
              var request = context.Request;
              var stream = request.Body;// currently holds the original stream                    
              var originalReader = new StreamReader(stream);
              var originalContent = await originalReader.ReadToEndAsync();
              var notModified = true;
              try
              {
  
                  if (originalContent != null)
                  {
                      var json = &quot;This is just a string as deserializing returns string&quot;;
                      //modified stream
                      var requestData = Encoding.UTF8.GetBytes(json);
                      stream = new MemoryStream(requestData);
                      notModified = false;
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.Message);
              }
              if (notModified)
              {
                  //putting original data
                  var requestData = Encoding.UTF8.GetBytes(originalContent);
                  stream = new MemoryStream(requestData);
              }
  
              request.Body = stream;
              
              await next(context);
          }
  </code></pre>
  <p>So the <strong>working code</strong> pass on the changes to the controller, and has value <em>Id =&quot;Changed in middlware&quot;</em> and <em>Email = &quot;changed&quot;</em> but the <strong>not working code</strong> doesnt pass anything ... in the controller, argument has null and not the <em>&quot;This is just a string as deserializing returns string&quot;</em> value. I know this is not some magic happening. Am i missing something?</p>