0

I'm trying to make a CRUD REST API with Flask en SQlALchemy. I'm making request with the curl command on the linux shell but I cant get it to work. If I open a python shell and add the things manually they get added to the database.

<!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 
davidism
  • 121,510
  • 29
  • 395
  • 339
Eric Murta
  • 35
  • 4
  • Please give a proper error description. – Klaus D. Apr 14 '17 at 04:04
  • I cannot reproduce this error. It works in my tests. – mechanical_meat Apr 14 '17 at 04:09
  • lol, thats the error, it's saying that my server didnt understand my curl request. I'm almost sure its something wrong with my curl request – Eric Murta Apr 14 '17 at 04:15
  • Possible duplicate of [What is the cause of the Bad Request Error when submitting form in Flask application?](http://stackoverflow.com/questions/14105452/what-is-the-cause-of-the-bad-request-error-when-submitting-form-in-flask-applica) – Priyank Mehta Apr 14 '17 at 05:49

1 Answers1

0

the issue is that Flask raises an HTTP error when it fails to find a key in the args and form dictionaries. 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.

check:

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

change your create view to the following code and check output:

@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'
Community
  • 1
  • 1