Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORSMiddleware is not working #810

Closed
ghost opened this issue Jan 27, 2020 · 2 comments
Closed

CORSMiddleware is not working #810

ghost opened this issue Jan 27, 2020 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 27, 2020

So, I went through documentation and I made this test application, to see if the CORSMiddleware even works. Well, apparently it doesn't work at all, the headers are not being sent and the OPTIONS request still returns 405.

Is it just me misunderstanding something and if so, what exactly? If it's not just me, then I think something is broken here.

I would really appreciate some TIP on that, since I rewrote one of my API wrappers to starlette and it doesn't really work, because of cors ;-;

My application code

from starlette.applications import Starlette
from starlette.responses import JSONResponse, PlainTextResponse
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
from starlette.exceptions import HTTPException


middleware = [
    Middleware(CORSMiddleware, allow_origins=['*'], allow_methods=["GET", "OPTIONS"],
               allow_headers=["Token", "Origin", "X-Requested-With", "Content-Type", "Accept"])
]

app = Starlette(middleware=middleware)


@app.route('/')
async def homepage(request):
    return JSONResponse(content={"message": "hi"})


@app.route('/test')
async def test(request):
    return JSONResponse(content={"test": "the test route"})

Here's results of the request to root:
Postman_e6qkuwYQR2
image

@Turall
Copy link

Turall commented Jan 29, 2020

I have same problem.

@tomchristie
Copy link
Member

I'd suggest looking through https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS closely as a first pass if you're having any issues with CORS support.

The outgoing request needs to include an "Origin" header in order to prompt CORS headers in the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants