用烧瓶发布请求错误

时间:2017-04-14 03:53:54

标签: python curl flask

我正在尝试使用Flask和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=juliana@.com' http://localhost:5000/create 

1 个答案:

答案 0 :(得分:0)

  

问题是当Flask无法在args中找到密钥并形成字典时,会引发HTTP错误。 Flask默认假设的是,如果您要求某个特定密钥并且它不在那里,那么请求中就会遗漏一些内容,整个请求都无效。

检查:

  1. What is the cause of the Bad Request Error when submitting form in Flask application?
  2. Form sending error, Flask
  3. 将您的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'