title: Updating the response body in middleware .NET Core
body: <p>I have a custom middleware in my .NET Core 3.1 application, and trying to set the response StatusCode and Body like this:</p>
  <pre><code>public async Task Invoke(HttpContext context)
  {
      if ( &lt;some condition on context.Request&gt; )
      {
          context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
          await context.Response.WriteAsync(&quot;my custom message&quot;);
  
          // Bad request, do not call next middleware.
          return;
      }
  
      // Call next middleware.
      await _requestDelegate(context);
  }
  </code></pre>
  <p>With the above code, the StatusCode is correctly set, however, the response Body is empty. How can I write my custom message to the Body?</p>
  <p><strong>Update1:</strong></p>
  <p>Added <code>await</code>, but this won't solve the issue. Thanks @Jonesopolis for mentioning that.</p>
  <p><strong>Update 2</strong></p>
  <p>So I was testing the response in Swagger (I was also looking at the developer's Network tab). However, when I tested in Postman, I was getting the expected response body.</p>
  <p>So the question really is why the response body wasn't showing up in Swagger/network tab?</p>
  <p>Thanks!</p>