rest - werkzeug.exceptions.BadRequestKeyError : 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'id'

标签 rest api post flask

html页面

{%block title%}Login page{% endblock %}

{%block content%}
<form action = '#' method="post">
   <p>creds:</p>
   <p><input type="number"  placeholder="id"  Id="id" /></p>
   <p><input type="text"  placeholder="nm"  name="nm" /></p>
   <p><input type="submit" value="submit" /></p>
</form>
{%endblock%}

应用代码
@app.route("/")
def home():
    return render_template("login.html")

@app.route("/",methods = ["POST","GET"])
def post():
    if request.method == "POST":
        user = request.form['nm']
        id = request.form['id']
        sql = ('''INSERT INTO abc
                (id, name) VALUES (?, ?)
                ''')
        val = (id,user)
        cur.execute (sql, val)
    return 'Ok'

我尝试使用 return.form.get('id') 但它返回 null

任何人都可以帮我解决这个问题吗

谢谢

最佳答案

<p><input type="number"  placeholder="id"  name="id" /></p>

您输入的是 ID 而不是名称

关于rest - werkzeug.exceptions.BadRequestKeyError : 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60599763/

相关文章:

javascript - jQuery:通过 ajax 将附加数组添加到序列化表单数据并使用 PHP 访问它?

java - 使用 RestTemplate 作为 Rest API 时出现 400 错误代码

python - django-piston 够成熟吗?

php - 如何在 laravel 中从一个函数开始事务并在另一个函数中结束它

ios - swift API认证

post - cURL 有效,但没有其他客户端使用 Go sebserver

android - 网络pojo类和数据库实体类,如何避免重复

rest - 在 http 请求中设置 grpc 元数据

node.js - 通过 rest api 训练 Microsoft Custom Vision 模型

javascript - 表单发布不接受链接