以更好的方式处理带有可变信息的 POST 请求......使用 Flask
Handle a POST request with variable information, in a better way... using Flask
我有一个带有 4 个 bootstrap 按钮的客户端应用程序,我想读取每个按钮的状态。打开按钮将发送 "L1/2/3/4ON" 的适当 POST 请求。我已经这样做了;
@app.route("/param=L3ON", methods=['POST'])
def handle_L3():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L2ON", methods=['POST'])
def handle_L2():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L1ON", methods=['POST'])
def handle_L1():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L4ON", methods=['POST'])
def handle_L4():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
**strong text**return 'OK'
我的 javascript 代码(在客户端)就像;
function ON(value) {
var request = new XMLHttpRequest();
if (value==="L1") {
request.open("POST","param=L1ON", true);
}else if (value==="L2") {
request.open("POST","param=L2ON", true);
}else if (value==="L3") {
request.open("POST","param=L3ON", true);
}else if (value==="L4") {
request.open("POST","param=L4ON", true);
}
request.send(null);
}
我正在寻找一种更好的方法,而不是为每个单独的处理程序。有没有一种方法可以检查 POST 请求的某些部分,即 @app.route("/param=", methods=['POST']),然后我可以进一步检查哪个请求是通过使用 "request" 在该请求中找到适当的字符吗?
您可以使用 URL 个转换器:
@app.route('/param=<name>')
def handle(name):
if request.method == 'POST':
if name == 'L1ON':
#do something
elif name == 'L4ON':
#do something
return 'ok'
我有一个带有 4 个 bootstrap 按钮的客户端应用程序,我想读取每个按钮的状态。打开按钮将发送 "L1/2/3/4ON" 的适当 POST 请求。我已经这样做了;
@app.route("/param=L3ON", methods=['POST'])
def handle_L3():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L2ON", methods=['POST'])
def handle_L2():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L1ON", methods=['POST'])
def handle_L1():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
return 'OK'
@app.route("/param=L4ON", methods=['POST'])
def handle_L4():
if request.method == 'POST':
#########################
# DO SOMETHING
#########################
**strong text**return 'OK'
我的 javascript 代码(在客户端)就像;
function ON(value) {
var request = new XMLHttpRequest();
if (value==="L1") {
request.open("POST","param=L1ON", true);
}else if (value==="L2") {
request.open("POST","param=L2ON", true);
}else if (value==="L3") {
request.open("POST","param=L3ON", true);
}else if (value==="L4") {
request.open("POST","param=L4ON", true);
}
request.send(null);
}
我正在寻找一种更好的方法,而不是为每个单独的处理程序。有没有一种方法可以检查 POST 请求的某些部分,即 @app.route("/param=", methods=['POST']),然后我可以进一步检查哪个请求是通过使用 "request" 在该请求中找到适当的字符吗?
您可以使用 URL 个转换器:
@app.route('/param=<name>')
def handle(name):
if request.method == 'POST':
if name == 'L1ON':
#do something
elif name == 'L4ON':
#do something
return 'ok'