Friday, January 18, 2019

MongoDB: Exception Handling

Def

Code
=============================
from flask import Flask
from flask import jsonify
from flask import request
from flask_pymongo import PyMongo
from bson.json_util import dumps

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'esawal'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/esawal'

mongo = PyMongo(app)


@app.route('/', methods=['POST'])
def add_star():
  try:
    star = mongo.db["customers"]
    name = request.json['name']
    address = request.json['address']
    if name and address:
        star_id = star.insert({'name': name, 'address': address})
    return dumps({'message': 'SUCCESS'})
  except Exception:
    return dumps({'error' : "400"})

if __name__ == '__main__':
    app.run(debug=True)
=============================


RESULT
===============================

===============================

No comments:

Post a Comment