Skip to content

How to configure CORS in gunicorn #3001

Answered by jtvlrm
jtvlrm asked this question in Q&A
Discussion options

You must be logged in to vote

Well, I have the answer.
The issue is that in browsers, when there are cross-domain requests, they usually send a preflight request to check the CORS configuration of the server. What needs to be done in the Python application is to add the following code at the beginning of the file. This will handle the response appropriately.

if environ['REQUEST_METHOD'] == 'OPTIONS':
    # Respuesta para las solicitudes preflight (OPTIONS)
    response_headers = [ ('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Methods', 'POST, OPTIONS'), ('Access-Control-Allow-Headers', 'Content-Type') ]
    start_response('200 OK', response_headers)
    print("a responder")
    return []

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@benoitc
Comment options

Answer selected by jtvlrm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants