邮递员无法发布负数,怎么办?

Posted

技术标签:

【中文标题】邮递员无法发布负数,怎么办?【英文标题】:Not being able to POST negative numbers by Postman, how to do that? 【发布时间】:2022-01-14 15:41:30 【问题描述】:

我有一个 Python 应用程序,它使用 Postman 来 GET 和 POST 数据到/从 MongoDB。这是一个简单的 GasStation 应用程序,但它让我们发布负数。如何验证不允许负数?

@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

【问题讨论】:

【参考方案1】:

你可以像这样比较它们:

@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

【讨论】:

谢谢,它有效! 没问题!祝你好运! :)

以上是关于邮递员无法发布负数,怎么办?的主要内容,如果未能解决你的问题,请参考以下文章

ABAP选择屏幕字段无法输入负值

ABAP选择屏幕字段无法输入负值

javascript 负数 怎么取 让正数变成负数

标头名称必须是邮递员中的有效 HTTP 令牌 ["Authorization"]

剪影分数怎么可能是负数?

上传音频文件时,API 在邮递员中工作,但在 android 应用程序中不工作