From 6051c052219dc9efe183e92575f4dfbd170e177f Mon Sep 17 00:00:00 2001 From: Caozhenhui <823914102@qq.com> Date: Tue, 9 Nov 2021 13:55:43 +0800 Subject: [PATCH] Resolved ERROR:PUT method not found --- myems-api/app.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/myems-api/app.py b/myems-api/app.py index 0e652872..5bfe93c8 100644 --- a/myems-api/app.py +++ b/myems-api/app.py @@ -91,12 +91,34 @@ from reports import virtualmetercost # https://github.com/lwcolton/falcon-cors # https://github.com/yohanboniface/falcon-multipart -cors = CORS(allow_all_origins=True, - allow_credentials_all_origins=True, - allow_all_headers=True, - allow_all_methods=True) -api = falcon.App(middleware=[falcon.CORSMiddleware(allow_origins='*', expose_headers=['Allow']), MultipartMiddleware()]) +class CORSComponent: + def process_response(self, req, resp, resource, req_succeeded): + resp.set_header('Access-Control-Allow-Origin', '*') + + if (req_succeeded + and req.method == 'OPTIONS' + and req.get_header('Access-Control-Request-Method') + ): + # NOTE: This is a CORS preflight request. Patch the + # response accordingly. + + allow = req.get_header('Access-Control-Request-Method') + # resp.delete_header('Allow') + + allow_headers = req.get_header( + 'Access-Control-Request-Headers', + default='*' + ) + + resp.set_headers(( + ('Access-Control-Allow-Methods', allow), + ('Access-Control-Allow-Headers', allow_headers), + ('Access-Control-Max-Age', '86400'), # 24 hours + )) + + +api = falcon.App(middleware=[CORSComponent(), MultipartMiddleware()]) ######################################################################################################################## # Routes for System Core