I have a Python app that uses Postman to GET and POST data's to/from MongoDB. It is a simple GasStation app, but it let's us POST negative numbers. How to validate that doesn't allow negative numbers?
@server_api.route('/add_data', methods=['POST'])
def receive_data():
data_received = request.get_json()
if "PetrolCapacity" not in data_received or "DieselCapacity" not in data_received:
return Response("Bad arguments", 400)
print(data_received)
service.push_to_database(data_received)
return Response("Data received successfully", 200)
@server_api.route('/get_data', methods=['GET'])
def return_data():
last_element = service.query_last_element()
print(last_element)
data = last_element['PetrolCapacity']
return data
CodePudding user response:
You can just compare them like this:
@server_api.route('/add_data', methods=['POST'])
def receive_data():
data_received = request.get_json()
if "PetrolCapacity" not in data_received or "DieselCapacity" not in data_received:
return Response("Bad arguments", 400)
petrol_capacity = data_received["PetrolCapacity"]
for tank in petrol_capacity:
if petrol_capacity[tank] < 0:
return Response("Negative values not allowed", 400)
if data_received["DieselCapacity"] < 0:
return Response("Negative values not allowed", 400)
print(data_received)
service.push_to_database(data_received)
return Response("Data received successfully", 200)
@server_api.route('/get_data', methods=['GET'])
def return_data():
last_element = service.query_last_element()
print(last_element)
data = last_element['PetrolCapacity']
return data