2017-04-14 137 views
0

我正在尝试使用Flask en SQlALchemy制作CRUD REST API。我正在使用linux shell的curl命令提出请求,但是我不能让它工作。如果我打开一个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 protected]' http://localhost:5000/create 
+0

请给一个适当的错误说明ription。 –

+0

我无法重现此错误。它在我的测试中起作用。 – bernie

+0

大声笑,这就是错误,这是说我的服务器不理解我的卷发请求。我几乎可以肯定它的错误,我的curl请求 –

回答

0

的问题是,当瓶它未能找到在ARGS键和形成字典引发HTTP错误。 Flask假定默认情况下,如果您要求特定的密钥并且它不存在,那么请求中会有一些内容被忽略,并且整个请求都是无效的。

检查:

  1. What is the cause of the Bad Request Error when submitting form in Flask application?
  2. Form sending error, Flask

改变你的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'