Azure Function为req body参数提供默认值

时间:2018-04-16 12:48:47

标签: azure azure-functions

我有一个HTTP触发器Azure函数,请求正文中有大约10个参数,字符串整数和布尔值。 调用时,并非所有参数都具有值。在代码中我需要检查一个是否为空,然后根据结果执行不同的操作。 问题是空的req参数会产生错误。 我希望字符串参数为“”,当它们为空时,整数0和布尔值为假,但我对Azure很新,不知道如何给它们默认值。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

正如Marie所指出的,Azure Functions有一个如何map a request body to a POCO的例子。将通过反序列化请求主体JSON来填充此POCO上的字段。如果您在此POCO中包含stringboolint类型的属性,则如果您未在请求正文中包含这些字段,则应使用各自的默认值填充这些属性。

答案 1 :(得分:0)

您可以使用host.json文件中的默认值执行此操作。

"queues": {
        "description": "Configuration settings for 'queue' triggers.",
        "type": "object",

        "properties": {
            "maxPollingInterval": {
                "description": "The maximum interval in milliseconds between queue polls.",
                "type": "integer",
                "default": 1000
            },

            "batchSize": {
                "description": "The number of queue messages to retrieve and process in parallel (per job function).",
                "type": "integer",
                "maximum": 32,
                "minimum": 1,
                "default": 16
            },

            "maxDequeueCount": {
                "description": "The number of times to try processing a message before moving it to the poison queue",
                "type": "integer",
                "default": 5
            },

            "newBatchThreshold": {
                "description": "The threshold at which a new batch of messages will be fetched. The default is batchSize/2.",
                "type": "integer"
            },

            "visibilityTimeout": {
                "description": "The visibility timeout that will be applied to messages that fail processing.",
                "pattern": "^\\d\\d:\\d\\d:\\d\\d$",
                "default": "00:00:00"
            }
        }
    }

架构参考:http://json.schemastore.org/host