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:
myDB = mongo.db["customers"]
try:
if not request.json['name']:
return jsonify({'responseCoe': 400, 'message': 'Name is Empty'})
except KeyError:
return jsonify({'responseCoe': 400, 'message': 'Name not found'})
try:
if not request.json['address']:
return jsonify({'responseCode': 400, 'message': 'Address is Empty'})
except KeyError:
return jsonify({'responseCoe': 400, 'message': 'Address not found'})
name = request.json['name']
address = request.json['address']
star_id = myDB.insert_one({'name': name, 'address': address})
output = []
for s in myDB.find():
output.append({'name': name, 'address': address})
return jsonify({'result': output})
except Exception:
return dumps({'error': "400"})
if __name__ == '__main__':
app.run(debug=True)
==================================
REQUEST & RESPONSE
==================================
==================================
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:
myDB = mongo.db["customers"]
try:
if not request.json['name']:
return jsonify({'responseCoe': 400, 'message': 'Name is Empty'})
except KeyError:
return jsonify({'responseCoe': 400, 'message': 'Name not found'})
try:
if not request.json['address']:
return jsonify({'responseCode': 400, 'message': 'Address is Empty'})
except KeyError:
return jsonify({'responseCoe': 400, 'message': 'Address not found'})
name = request.json['name']
address = request.json['address']
star_id = myDB.insert_one({'name': name, 'address': address})
output = []
for s in myDB.find():
output.append({'name': name, 'address': address})
return jsonify({'result': output})
except Exception:
return dumps({'error': "400"})
if __name__ == '__main__':
app.run(debug=True)
REQUEST & RESPONSE
==================================
==================================
No comments:
Post a Comment