首页 > 解决方案 > Azure API 管理 POST 请求 - 需要所有正文参数

问题描述

如何验证我的发布请求需要正文中的所有请求参数?我可以使用哪些政策表达方式?我正在使用以下表达式:

<policies>
<inbound>
    <base />
    <choose>
        <when condition="@((context.Request.Body) != null&& ((int)context.Request.Body.As<JObject>()["Id"])>0)">
            <return-response>
            </return-response>
        </when>
        <otherwise>
            <return-response>
            </return-response>
        </otherwise>
    </choose>
</inbound>

如何限制输入此帖子请求的所有正文参数?

标签: azureazure-api-management

解决方案


请参阅 APIM 的内容验证策略:https ://docs.microsoft.com/en-us/azure/api-management/validation-policies#validate-content它们允许您验证该请求实际上是否符合规范中指定的架构。

作为替代方案,您可以将 body 读取为 JObject 并手动检查每个感兴趣的属性。请注意,您要使用 context.Request.Body.As( preserveContent: true ) 来确保正文被缓存并可用于稍后发送到后端。


推荐阅读