Making two HTTP POST Requests in Flask

前端 未结 1 1785
悲哀的现实
悲哀的现实 2021-01-29 09:42

I need to do two post request as shown in following code but only one POST is working,below is the python code :

import json
from flask import Flask, render_tem         


        
1条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 10:18

    Both POSTs are probably working but you return after checking the first one.

    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
    

    Instead, you need to collect both results and return them together.

    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)
    

    0 讨论(0)
提交回复
热议问题