July 2017
Intermediate to advanced
374 pages
8h
English
In this book, we go with the first approach to the POST method. So, let's define our route for the post method in app.py, and call the function to update the user record to the database file, as follows:
@app.route('/api/v1/users', methods=['POST'])
def create_user():
if not request.json or not 'username' in request.json or not 'email' in request.json or not 'password' in request.json:
abort(400)
user = {
'username': request.json['username'],
'email': request.json['email'],
'name': request.json.get('name',""),
'password': request.json['password']
}
return jsonify({'status': add_user(user)}), 201
As you can see, in the preceding method, we called the exception with error code 400; let's write its handler now:
@app.errorhandler(400) ...