Flask-Restful Interrupt Method Ignores CORS Options

I have a Flask-Restful API configured with some CORS options:

api = Api()
api.decorators=[cors.crossdomain(origin='*', headers=['accept', 'Content-Type'])]

...

api.init_app(app)

My API accepts requests POSTthat may fail if the data in the request is invalid:

class myAPI(Resource):
    def post(self):
        args = request.get_json()
        if args.get('something'):
            return {'message': 'Request worked, data received!',
                    'something': args['something']}
        else:
            abort(500, "Error: Data must contain a 'something' field!")

When I make a successful request POSTto my API, I see that the CORS parameters are set correctly:

...
* upload completely sent off: 81 out of 81 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 205
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: HEAD, GET, POST, OPTIONS
< Access-Control-Max-Age: 21600
< Access-Control-Allow-Headers: ACCEPT, CONTENT-TYPE
< Server: Werkzeug/0.9.4 Python/2.7.6

If, however, the post-call in my class leaves the interrupt method (by intentionally sending incorrect data to the request), then all fields are Access-Control-*absent in the response :

* upload completely sent off: 75 out of 75 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 500 INTERNAL SERVER ERROR
< Content-Type: application/json
< Content-Length: 51
< Server: Werkzeug/0.9.4 Python/2.7.6

Is it possible for the method to abortwork with my CORS rules, or should I create my own full answer and not use the interrupt function?

+4
1

, , HTTP, , .

CORS, Flask-Cors, , , CORS , .

(E.G. 500), , after_request .

, Flask-Cors.

+3

Source: https://habr.com/ru/post/1542513/


All Articles