3
JSON错误
我想如下添加到API一个DELETE方法:尝试除瓶
if request.method == 'DELETE':
if request.headers['Content-Type'] == 'application/json':
try:
data = json.loads(request.data)
data_id = data['id']
db.execute('DELETE FROM places WHERE id=' + data_id)
db.commit()
resp = Response({"Delete Success!"}, status=200, mimetype='application/json')
return resp
except (ValueError, KeyError, TypeError):
resp = Response({"JSON Format Error."}, status=400, mimetype='application/json')
return resp
我传递了以下卷曲:
curl -H "Content-type: applicaiton/json" -X DELETE http://localhost:5000/location -d '{"id":3}'
除了块的尝试失败因为某些原因。我无法检测到问题所在。任何想法如何我可以调试呢?
您是否看到异常被提出?另外,如果没有id为'data_id'的项,你可能会考虑返回404而不是400 – Andbdrew
它返回错误400,因此我相信会引发一个异常。 – darksky
或'contentType'? –