Python 未能从flask api获取注释

Python 未能从flask api获取注释,python,html,rest,flask,Python,Html,Rest,Flask,在尝试获取添加注释的文本时,我使用以下curl命令获取flask.request.form中的关键错误。我试图打印出flask.request.form,但它是空的。如何修复它 添加新注释的curl命令: curl -ib cookies.txt --header 'Content-Type: application/json' --request POST --data '{"text":"Comment sent from curl"}' http://localhos

在尝试获取添加注释的文本时,我使用以下curl命令获取flask.request.form中的关键错误。我试图打印出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>

将返回新注释字典的My 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
@app.route('/api/v1/p//comments/',方法=[“POST”])
def添加注释(postid):
db=model.get_db()
所有者=烧瓶。会话[“用户名”]
query=“在注释(commentid、所有者、postid、文本、已创建)中插入值(NULL、、、、、DATETIME('now'))”
db.execute(查询,(所有者、posted、flask.request.form[“text”]))
last\u id=db.execute(“选择last\u insert\u rowid()”).fetchone()[“last\u insert\u rowid()”]
获取最新的注释\u query=“从注释中选择*,其中注释ID=?”
comment=db.execute(获取最新的注释查询(最后一个id)).fetchone()
打印('获取注释:',注释)
返回烧瓶.jsonify(注释),201

您的HTML表单配置不正确

  • 您正在发送GET请求,而您的烧瓶仅接受POST
  • flask.request.form[“text”]
    要求输入名为text的文本,但您的文本框没有任何名称
  • 没有提交按钮
  • 您可以通过以下方式进行修复:

    <form id="comment-form" method="post">
        <input type="text" value="" name="text" />
        <input type="submit" />
    </form>
    

    希望这有帮助。祝您好运。

    您的HTML表单配置不正确

  • 您正在发送GET请求,而您的烧瓶仅接受POST
  • flask.request.form[“text”]
    要求输入名为text的文本,但您的文本框没有任何名称
  • 没有提交按钮
  • 您可以通过以下方式进行修复:

    <form id="comment-form" method="post">
        <input type="text" value="" name="text" />
        <input type="submit" />
    </form>
    

    希望这有帮助。祝你好运。

    在使用curl时添加@Harshal的答案,似乎你访问请求数据不正确。由于请求的内容类型设置为
    application/json
    ,因此需要使用
    flask.request.json
    -

    或者您可以更新
    curl
    命令,如下所示

    curl -ib cookies.txt   
      --request POST   
      --data-urlencode "text=Comment sent from curl"  
      http://localhost:8000/api/v1/p/3/comments/
    

    在这种情况下,curl将自动使用
    Content-Type-application/x-www-form-urlencoded
    ,您的应用程序将能够使用
    flask.request.form

    读取请求数据,在使用curl时添加到@Harshal的答案中,看起来您访问请求数据不正确。由于请求的内容类型设置为
    application/json
    ,因此需要使用
    flask.request.json
    -

    或者您可以更新
    curl
    命令,如下所示

    curl -ib cookies.txt   
      --request POST   
      --data-urlencode "text=Comment sent from curl"  
      http://localhost:8000/api/v1/p/3/comments/
    

    在这种情况下,curl将自动使用
    Content-Type-application/x-www-form-urlencoded
    ,您的应用程序将能够使用
    flask.request.form
    读取请求数据,谢谢!我把它改成了json[“text”],它成功了!非常感谢。我把它改成了json[“text”],它成功了!