2016-02-23 29 views
-1

我正在用Flask編寫一個REST API,它應該創建一個Dictionary of Dictionary,例如,如何使用curl將唯一值發佈到多個字典

Dictionary = { 
     dict1 = {}, 
     dict2 = {} 
     } 

我希望每個字典都有個別的值,如果可能,我想在一個請求中填寫兩個字符。

到目前爲止,我一直在用curl請求測試我的代碼,看起來它幾乎就在那裏...除了這兩個字符都被填充了相同的值集合。

api.py

dictionary = {} 

@app.route('/launch', methods=['POST']) 
def launch(): 
    gw_type = request.json['type'] 

    for t in gw_type: 
     dictionary[t] = { 
      'client': request.json['client'] 
      'band': request.json['band'] 
      'password': request.json['password'] 

    return jsonify(**dictionary) 

捲曲請求

curl -H "Content-Type: application/json" -X 
POST -d '{"type":["type1", "type2"], "client":["test1", "test2"], 
"bands":["ABCD", "ABC"], "password":["pass", "pass2"]}' 
http://localhost:5000/launch 

輸出

{ 
    "type1": { 
    "bands": [ 
     "ABCD", 
     "ABC" 
    ], 
    "client": [ 
    "test1", 
    "test2" 
    ], 
    "password": [ 
    "pass", 
    "pass2" 
    ] 
}, 
"type2": { 
    "bands": [ 
    "ABCD", 
    "ABC" 
    ], 
    "client": [ 
    "test1", 
    "test2" 
    ], 
    "password": [ 
    "pass", 
    "pass2" 
    ] 
} 
} 

如果可能的話,我將如何去創造多個字典('type'),以便每個TYPE在一個curl請求中擁有它自己的唯一值'client','band'和'password'?

感謝

回答

1

所以,你正在訪問clientbandspassword每次的完整列表。如果它們在curl命令命令你要那麼的方式,所有你需要做的是修改代碼以使用索引進行正確的值:

@app.route('/launch', methods=['POST']) 
def launch(): 
    gw_type = request.json['type'] 

    for i in range(len(gw_type)): 
     dictionary[gw_type[i]] = { 
      'client': request.json['client'][i] 
      'band': request.json['band'][i] 
      'password': request.json['password'][i] 

    return jsonify(**dictionary) 

這會得到第一個客戶爲第一類,第一第一類爲第二客戶端,第二類爲第二客戶端等。

+0

非常完美,非常感謝 - 我有一種感覺,我的for循環沒有做我想要的,我只是看不到如何修理它。 – User588233

相關問題