Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not being able to POST negative numbers by Postman, how to do that?

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

Postman Data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading