Why i am getting 400 Bad Request error when sending json data in Flask? -


i trying write small restful api application, using chrome postman extension sending requests app .

i believe code not have mistakes every time sending post request 400 bad request error raising , here code:

@api_route.route('/api', methods=['get']) def api():     return jsonify({'message':'api v1.0'})  @api_route.route('/api', methods=['post']) def create_user():     data = request.get_json()     if data:         hashed_password = generate_password_hash(data['password'], method='sha256')         api = api(email=data['email'], password=hashed_password)         db.session.add(api)         db.session.commit()         return jsonify({'message', 'new user created!'}) 

the json data sending looks this:

{"email" : "test", "password" : "123123123"}

why getting 400 error ??

update:

screenshots requests using postman:

get request get request

post request post request

here initiating api route inside api controller :

from flask import blueprint api_route = blueprint(     'api',     __name__ ) . import views 

then registering inside def create_app() function :

from .api import api_route app.register_blueprint(api_route) 

here extensions using in application:

toolbar = debugtoolbarextension() assets_env = environment() cache = cache() moment = moment() htmlminify = htmlmin() csrf = csrfprotect() jac = jac() googlemap = googlemaps() session = session() principal = principal() 

a approach structure views follows:

instead of creating view same route different request methods, can handle request methods in same view:

@api_route.route('/api', methods=['get', 'post']) def api():     if request.method == 'get':         return jsonify({'message':'api v1.0'})      else:         data = request.get_json(force=true)         if data:             hashed_password = generate_password_hash(data['password'], method='sha256')             api = api(email=data['email'], password=hashed_password)             db.session.add(api)             db.session.commit()             return jsonify({'message': 'new user created!'})          # in case if condition didn't satisfy         return none 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -