flask how to get the HTTP_ORIGIN of a request

后端 未结 3 1101
时光说笑
时光说笑 2021-02-06 07:43

I\'d like to make a response with the \"Access-Control-Allow-Origin\" header been set all by myself, while it\'s seems messing up to figure out where the \"HTTP_ORIGIN\" paramet

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-06 08:09

    here is example :

    from flask import request
    ...
    allow_origin_list = ['https://example.com', 'http://example.com']
    
    if 'HTTP_ORIGIN' in request.environ and request.environ['HTTP_ORIGIN']  in allow_origin_list:
        response.headers.add('Access-Control-Allow-Origin', request.environ['HTTP_ORIGIN'] )
        response.headers.add('Access-Control-Allow-Headers', 'access-control-allow-origin,content-type')
        response.headers.add('Access-Control-Allow-Methods', 'GET,POST')
    

提交回复
热议问题