尝试获取添加评论的文本时,我在 flask.request.form 中使用以下 curl 命令获取关键错误。我尝试打印出 flask.request.form 但它是空的。如何解决?
curl 命令添加新评论:
curl -ib cookies.txt
--header 'Content-Type: application/json'
--request POST
--data '{"text":"Comment sent from curl"}'
http://localhost:8000/api/v1/p/3/comments/
错误:
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'text'
我的评论对象:
<form id="comment-form">
<input type="text" value=""/>
</form>
我的 flask.api python 文件将返回新的评论字典:
@app.route('/api/v1/p/<int:postid>/comments/', methods=["POST"])
def add_comment(postid):
db = model.get_db()
owner = flask.session["username"]
query = "INSERT INTO comments(commentid, owner, postid, text, created) VALUES(NULL, ?, ?, ?, DATETIME('now'))"
db.execute(query, (owner, postid, flask.request.form["text"]))
last_id = db.execute("SELECT last_insert_rowid()").fetchone()["last_insert_rowid()"]
get_newest_comment_query = "SELECT * FROM comments WHERE commentid = ?"
comment = db.execute(get_newest_comment_query, (last_id,)).fetchone()
print('get comment: ', comment)
return flask.jsonify(comment), 201
慕少森
翻翻过去那场雪
相关分类