title: Why does fastapi hang when adding a middleware to print the HTTP request body?
body: <p>I am using a middleware to print the HTTP request body to avoid <code>print</code> statements in every function. However, there is no response from fastapi when running the client code.</p>
  <p>The server is simplified to the code below:</p>
  <pre><code>import typing
  
  import uvicorn
  from fastapi import FastAPI
  from fastapi import Request, Body
  
  app = FastAPI()
  
  
  @app.middleware('http')
  async def debug_request(request: Request, call_next):
      _body = await request.body()
      print(_body)
      #
      response = await call_next(request)
      return response
  
  
  @app.put(&quot;/&quot;)
  def _(
      _body: typing.Dict
  ):
      # print(_body)  # this statement is replaced by the middleware
      return {&quot;detail&quot;: &quot;ok&quot;}
  
  
  if __name__ == '__main__':
      uvicorn.run(app, host='localhost', port=8000)
  </code></pre>
  <p>The client code is given below:</p>
  <pre><code>import requests
  
  _url = 'http://localhost:8000/'
  _json = {
      'row_id': '1'
  }
  resp = requests.put(_url, json=_json)
  if not resp.ok:
      print('http-code: ', resp.status_code)
  print('http-response: ', resp.text)
  </code></pre>