【发布时间】:2017-01-27 10:26:10
【问题描述】:
我正在尝试将 JSON 数据发布到烧瓶应用程序。然后应用程序应该循环遍历数组中的每个对象并返回结果。如果数组中只有一个对象,我可以返回对象中每个值的结果。但是任何具有多个对象的 JSON 数据都会产生 500 Internal Server 错误。
我在这里缺少什么?
from flask import Flask, url_for
app = Flask(__name__)
from flask import request
from flask import json
@app.route('/messages', methods = ['POST'])
def api_message():
if request.headers['Content-Type'] == 'application/json':
foo = request.get_json()
output = ""
for i in foo['Location']:
Item_id = i['Item_id']
Price = i['Price']
output = output + Item_id + Price
# do stuff here later
return output
else:
return "415 Unsupported"
if __name__ == '__main__':
app.run()
我在一个终端中运行上述代码,当我将 JSON 数据发布到另一个终端时,我得到“500 Internal Server error”:
curl -H "Content-type: application/json" \ -X POST http://127.0.0.1:5000/messages -d '[{"Location":"1","Item_id":"12345","Price":"$1.99","Text":"ABCDEFG"},{"Location":"2","Item_id":"56489","Price":"$100.99","Text":"HIJKLMNO"},{"Location":"3","Item_id":"101112","Price":"$100,000.99","Text":"PQRST"}]'
【问题讨论】:
-
for i in foo: i['Location']。顺便说一句:在调试模式下运行,您应该会在网页上看到更多信息。
标签: python json flask flask-restful