title: How can I enable CORS in FastAPI?
body: <p>I'm trying to enable CORS in this very basic FastAPI example, however it doesn't seem to be working.</p>
  <pre class="lang-py prettyprint-override"><code>from fastapi import FastAPI
  from fastapi.middleware.cors import CORSMiddleware
  
  app = FastAPI()
  
  app.add_middleware(
      CORSMiddleware,
      allow_origins=['*']
  )
  
  @app.get('/')
  def read_main():
      return {'message': 'Hello World!'}
  </code></pre>
  <p>This is the response I get:</p>
  <pre><code>curl -v http://127.0.0.1:8000
  *   Trying 127.0.0.1...
  * TCP_NODELAY set
  * Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
  &gt; GET / HTTP/1.1
  &gt; Host: 127.0.0.1:8000
  &gt; User-Agent: curl/7.64.1
  &gt; Accept: */*
  &gt;
  &lt; HTTP/1.1 200 OK
  &lt; date: Fri, 08 Jan 2021 19:27:37 GMT
  &lt; server: uvicorn
  &lt; content-length: 26
  &lt; content-type: application/json
  &lt;
  * Connection #0 to host 127.0.0.1 left intact
  {&quot;message&quot;:&quot;Hello World!&quot;}*
  </code></pre>