2016-01-07 46 views
-1

HTTP POST請求我需要做兩個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是working.How我可以使兩個HTTP POST工作,任何人都可以建議?

enter image description here

+1

你的屏幕截圖並不真正顯示1 POST請求正在工作;你怎麼稱呼第二個請求?代碼或網絡信息在哪裏? – mguijarr

回答

2

兩個職位很可能工作,但你檢查後的第一個返回。

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) 
+0

是的,它工作....謝謝:) –