When trying to get the text of the added comments, I am getting the key error in flask.request.form with the following curl command. I try to print out flask.request.form but it was empty. How to fix it?
curl command to add new comment:
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/
ERROR:
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'text'
My comment obj:
<form id="comment-form">
<input type="text" value=""/>
</form>
My flask.api python file that would return new comment dictionary:
@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