在没有模板的情况下对 POST 的 Flask 适当响应?
Flask appropriate response for POST without template?
所以我有一种方法可以在数据库中为健身应用程序创建新练习。我正在使用 angular 来处理 ajax.
@app.route('/create', methods=['POST'])
def create():
data = request.get_json(request.data)
ex = Exercise(data['exName'], data['exType'])
db.session.add(ex)
db.session.commit()
提交成功后,最好的回复是什么?我真的不需要成功消息,只需要让客户知道它已成功创建。谢谢!
我想你可以发回 w3.org rfc2616 描述的 HTTP 204:
10.2.5 204 No Content
The server has fulfilled the request but does not need to
return an entity-body, and might want to return updated
metainformation. The response MAY include new or updated
metainformation in the form of entity-headers, which if
present SHOULD be associated with the requested variant.
我认为你可以在你的函数结束时这样做
return '', 204
我认为您应该发送 201 状态 (look here) 而不是 204,因为您刚刚创建了新实体。如果你想构建好的 RESTful API 你需要发送 201 和你之前创建的对象。
以后,在angular这边,你可以为所欲为。你会让用户知道在那里创建的练习。
所以我有一种方法可以在数据库中为健身应用程序创建新练习。我正在使用 angular 来处理 ajax.
@app.route('/create', methods=['POST'])
def create():
data = request.get_json(request.data)
ex = Exercise(data['exName'], data['exType'])
db.session.add(ex)
db.session.commit()
提交成功后,最好的回复是什么?我真的不需要成功消息,只需要让客户知道它已成功创建。谢谢!
我想你可以发回 w3.org rfc2616 描述的 HTTP 204:
10.2.5 204 No Content
The server has fulfilled the request but does not need to
return an entity-body, and might want to return updated
metainformation. The response MAY include new or updated
metainformation in the form of entity-headers, which if
present SHOULD be associated with the requested variant.
我认为你可以在你的函数结束时这样做
return '', 204
我认为您应该发送 201 状态 (look here) 而不是 204,因为您刚刚创建了新实体。如果你想构建好的 RESTful API 你需要发送 201 和你之前创建的对象。
以后,在angular这边,你可以为所欲为。你会让用户知道在那里创建的练习。