在Flask中发出两个HTTP POST请求

时间:2016-01-07 08:36:21

标签: javascript python flask

我需要做两个post请求,如下面的代码所示,但只有一个POST工作,下面是python代码:

import json
from flask import Flask, render_template, request, jsonify
import requests
app = Flask(__name__)


@app.route("/",methods=['GET','POST'])
def home():
    if request.method == 'POST':
        #user inputs
        value1 = request.form.get('first')
        value2 = request.form.get('second')
        value3 = request.form.get('third')




        #api call 
        url = 'http://apihere/myapi'
        payload = {
                    "perfid" : value1,
                    "section" : {
                        "hostname" : value2,
                        "iteration" : value3,
                        "sectionname" : "sysstat_M"
                        }
                }
        payload1 = {
                    "perfid" : value1,
                    "section" : {
                        "hostname" : value2,
                        "iteration" : value3,
                        "sectionname" : "sysstat_x_1sec"
                        }
                }

        r = requests.post(url, data=json.dumps(payload))
        returnData = {}
        r1 = requests.post(url, data=json.dumps(payload1))
        returnData1 = {}


        if r.status_code == 200:
            returnData["status"] = "SUCCESS"
            returnData["result"] = json.loads(r.text)
            return jsonify(returnData)
        else:
            returnData["status"] = "ERROR"
            return jsonify(returnData)

        if r1.status_code == 200:
            returnData1["status"] = "SUCCESS"
            returnData1["result"] = json.loads(r1.text)
            return jsonify(returnData1)
        else:
            returnData1["status"] = "ERROR"
            return jsonify(returnData1)

        #print(r.status_code, r.headers['content-type'])
        #print(r.text)

    return render_template('index.html')



@app.route("/filer",methods=['GET','POST'])
def filer():
    if request.method == 'POST':
        #user inputs
        value = request.form.get('first')
        #api call 
        url = 'http://myanother/api'.format(value)
        response = requests.get(url)
        returnData = {}
        if response.status_code == 200:
            returnData["status"] = "SUCCESS"
            returnData["result"] = json.loads(response.text)
            return jsonify(returnData)
        else:
            returnData["status"] = "ERROR"
            return jsonify(returnData)
        #jsonify(response.json())
    return render_template('filer.html')


if __name__ == '__main__':
    app.run(debug=True)

如下面的屏幕截图所示,只有一个POST工作正常。如何使HTTP POST工作,有人可以建议???

enter image description here

1 个答案:

答案 0 :(得分:2)

两个POST都可能正常工作,但您在检查完第一个后返回。

if r.status_code == 200:
    returnData["status"] = "SUCCESS"
    returnData["result"] = json.loads(r.text)
    return jsonify(returnData)  # HERE
else:
    returnData["status"] = "ERROR"
    return jsonify(returnData)  # OR HERE

if r1.status_code == 200:
    returnData1["status"] = "SUCCESS"
    returnData1["result"] = json.loads(r1.text)
    return jsonify(returnData1)  # You'd do it again here...
else:
    returnData1["status"] = "ERROR"
    return jsonify(returnData1)  # ... or here

相反,您需要收集两个结果并将它们一起返回。

if r.status_code == 200:
    returnData["status"] = "SUCCESS"
    returnData["result"] = json.loads(r.text)
else:
    returnData["status"] = "ERROR"

if r1.status_code == 200:
    returnData1["status"] = "SUCCESS"
    returnData1["result"] = json.loads(r1.text)
else:
    returnData1["status"] = "ERROR"

return jsonify(returnData=returnData, returnData1=returnData1)