[英]post request error with flask
I'm trying to make a CRUD REST API with Flask en SQlALchemy. 我正在尝试使用Flask en SQlALchemy制作CRUD REST API。 I'm making request with the curl command on the linux shell but I cant get it to work.
我在linux shell上使用curl命令发出请求,但无法正常工作。 If I open a python shell and add the things manually they get added to the database.
如果我打开python shell并手动添加东西,它们将被添加到数据库中。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
@app.route('/create', methods = ['POST'])
def create():
username = request.form['username']
age = request.form['age']
email = request.form['email']
curl -X POST -F 'username=Juliana' -F 'age=87' -F 'email=juliana@.com' http://localhost:5000/create
the issue is that Flask raises an HTTP error when it fails to find a key in the args and form dictionaries.
问题是,当Flask在args中找不到键并形成字典时,它会引发HTTP错误。 What Flask assumes by default is that if you are asking for a particular key and it's not there then something got left out of the request and the entire request is invalid.
Flask默认情况下假设的是,如果您要的是特定密钥,而该密钥不存在,则请求中遗漏了某些内容,整个请求均无效。
check: 校验:
change your create
view to the following code and check output: 将您的
create
视图更改为以下代码并检查输出:
@app.route('/create', methods = ['POST'])
def create():
username = request.form.get('username', '')
age = request.form.get('age', '')
email = request.form.get('email', '')
print username, age, email
# dbcreate = Profile(username, age, email)
# db.session.add(dbcreate)
# db.session.commit()
# print 'User Created'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.