我正在使用带有flask的无服务器框架来构建微服务。
问题是我的POST方法中的所有API总是以 HTML格式返回500,这与其他情况导致Lambda导致内部错误的情况完全不同。同时,使用GET方法的所有API都可以,并且它们都可以与AWS RDS连接。
import json
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/uploads", methods=['POST'])
def uploads():
data = request.json
param = {
...
}
with Database() as db:
with db.connect() as conn:
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
cur.execute("""
...
""", param)
response = {
"statusCode": 200,
"headers": {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': True,
},
"body": json.dumps(param)
}
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True)
这是错误消息:
Thu Dec 19 13:50:59 UTC 2019 : Endpoint response headers: {Date=Thu, 19 Dec 2019 13:50:59 GMT, Content-Type=application/json, Content-Length=472, Connection=keep-alive, x-amzn-RequestId=f7640b11-b108-45eb-afb7-af817ee44523, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5dfb8043-81d480a4f3b3dee77a09b712;sampled=0}
Thu Dec 19 13:50:59 UTC 2019 : Endpoint response body before transformations: {"statusCode": 500, "multiValueHeaders": {"Content-Type": ["text/html"], "Content-Length": ["290"], "Access-Control-Allow-Origin": ["*"]}, "body": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error</h1>\n<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>\n", "isBase64Encoded": false}
Thu Dec 19 13:50:59 UTC 2019 : Method response body after transformations: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
Thu Dec 19 13:50:59 UTC 2019 : Method response headers: {Content-Type=text/html, Content-Length=290, Access-Control-Allow-Origin=*, X-Amzn-Trace-Id=Root=1-5dfb8043-81d480a4f3b3dee77a09b712;Sampled=0}
Thu Dec 19 13:50:59 UTC 2019 : Successfully completed execution
Thu Dec 19 13:50:59 UTC 2019 : Method completed with status: 500
我已经尝试过:
调整响应格式,与返回的GET方法相同->不起作用
VPC与安全组和数据库连接->无法正常工作,其他GET方法可以同时很好地工作
缺少环境模块->不,此功能没有排除包
CORS->我今天唯一调整的内容->恢复为以前的设置,仍然无法正常工作
我奋斗了几个小时,仍然不知道。感谢任何建议。