How to set CORS Policy in AIOHTTP

Viewed 3492

I have added 'cors_middleware' but still get "has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." error.

#Code

root_app = web.Application(
    middlewares=[
        cors_middleware(
            allow_all=True,
            origins='*',
            # urls=[re.compile(r"^\/api")],
            allow_credentials=True,
            expose_headers="*",
            allow_headers='*',
            allow_methods=["POST", "PATCH", 'GET','OPTION'],
        ),
    ]
)

#Error

Access to XMLHttpRequest at 'http://localhost:8000/api/v1/user/' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

1 Answers

aiohttp-cors github

 import aiohttp_cors
 from aiohttp import web

 app = web.Application()
 app.router.add_post("/offer", offer)
    
 cors = aiohttp_cors.setup(app, defaults={
   "*": aiohttp_cors.ResourceOptions(
        allow_credentials=True,
        expose_headers="*",
        allow_headers="*"
    )
  })

  for route in list(app.router.routes()):
    cors.add(route)

in my case, solved this way

Related