azure-api-management - 在 Azure APIM 中验证请求参数
问题描述
我定义了一个full-name
接受名字和姓氏的 API,并将它们组合起来返回全名:
https://my-org.azure-api.net/my-app/full-name?firstName=John&lastName=Smith
# Returns: John Smith
在 Azure APIM 中,我在 API 架构中将两者firstName
和lastName
参数都设置为“必需”。
在入站策略中,我想验证请求中是否存在两者。我发现validate-parameters政策似乎就是这样做的:
<inbound>
<validate-parameters specified-parameter-action="prevent"
unspecified-parameter-action="detect"
errors-variable-name="validationErrors" />
</inbound>
令我惊讶的是,这没有任何赦免!我必须对每个参数进行手动检查(这段代码太冗长了):
<inbound>
<choose>
<when condition="@( context.Request.OriginalUrl.Query.GetValueOrDefault("firstName") == null )">
<return-response>
<set-status code="400" reason="Bad Request" />
<set-header name="Context-Type" exists-action="override">
<value>text/plain</value>
</set-header>
<set-body>'firstName' is required</set-body>
</return-response>
</when>
<when condition="@( context.Request.OriginalUrl.Query.GetValueOrDefault("lastName") == null )">
<return-response>
<set-status code="400" reason="Bad Request" />
<set-header name="Context-Type" exists-action="override">
<value>text/plain</value>
</set-header>
<set-body>'lastName' is required</set-body>
</return-response>
</when>
<otherwise>
</otherwise>
</choose>
</inbound>
有没有更方便的方法来验证 Azure APIM 中 API 架构的请求?
解决方案
推荐阅读
- django - 在 django 中删除帖子后,帖子图像仍在目录中
- jquery - 使用 Jquery 在元素顶部返回元素
- java - 使用 JAVA 通过 XML 阅读器读取 Excel 2013 版本文件时出现异常
- c# - EfCore 3.1 尝试添加已存在的列
- javascript - 反应本机firebase getToken()不起作用
- c# - 无法使用 luis 退出机器人退出功能
- mongodb - 在 MongoDB 中将日期递增到日期
- wordpress - 如何阻止其他人在 url 中插入变量
- json - 当从数据库中选择行数=0时,轴#0的数据列不能是谷歌条形图中的字符串×类型
- json - 如何在mysql中为json创建索引?