title: ASP.NET Core modify/substitute request body
body: <p>I want to do a substitution on <code>HttpContext.Request.Body</code>.</p>
  <p>I've tried to do it inside a middleware:</p>
  <pre><code>public async Task Invoke(HttpContext context)
  {
      if (context.Request.Path.Value.Contains(&quot;DataSourceResult&quot;))
      {
          var originalBody = new StreamReader(context.Request.Body).ReadToEnd();
          DataSourceRequest dataSource = null;
  
          try
          {
              dataSource = JsonConvert.DeserializeObject&lt;DataSourceRequest&gt;(originalBody);
          } catch
          {
              await _next.Invoke(context);
          }
  
          if (dataSource != null &amp;&amp; dataSource.Take &gt; 2000)
          {
              dataSource.Take = 2000;
  
              var bytesToWrite = dataSource.AsByteArray();
              await context.Request.Body.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
          }
          else
          {
              var bytesToWrite = originalBody.AsByteArray();
              await context.Request.Body.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
          }
      }
  
      await _next.Invoke(context);
  }
  </code></pre>
  <p>The first problem is that the body can be read only once, and secondly, the stream is read-only and can't be written to.</p>
  <p>How can I modify/substitute <code>Request.Body</code>? I need to change property value of request body.</p>