flask post獲取前端請求參數的三種方式總結

flask post獲取前端請求參數方式

post請求python flask獲取前端發來的參數有三種方式

1、放在params裡面

from flask import request
# params拿請求
data = {}
for key in request.args:
    data[key] = request.args.get(key)
 
file_type = data['file_type']
safety_check_command = data['safety_check_command']
remark = data['remark']
create_user = data['create_user']
weight = data['weight']

2、帶附件的 form-data

註意:不帶附件,前端服務器express+pm2可能出現問題。

from flask import request
 
file_obj = request.files.get('excel')
DeploymentID = request.form['DeploymentID']  # 獲取DeploymentID
environment = request.form['environment']  # 獲取環境
project = request.form['project']  # 獲取項目

3、json格式

from flask import request, json
 
data = request.get_data()
json_data = json.loads(data.decode("UTF-8"))
 
DeploymentID = json_data.get("DeploymentID")
Gitlab_URL = json_data.get("Gitlab_URL")
basics_URL = json_data.get("basics_URL")

總結

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: