你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于:所有 API 管理层级
              set-method 策略用于更改请求的 HTTP 请求方法。
注意
按照策略声明中提供的顺序设置策略的元素和子元素。 详细了解如何设置或编辑 API 管理策略。
策略语句
<set-method>HTTP method</set-method>
元素的值指定 HTTP 方法,例如 POST、GET 等。 允许使用策略表达式。
使用情况
示例
如果 HTTP 响应代码大于或等于 500,则此示例会使用 set-method 策略将消息发送到 Slack 聊天室。 有关此示例的详细信息,请参阅通过 Azure API 管理服务使用外部服务。
<choose>
    <when condition="@(context.Response.StatusCode >= 500)">
      <send-one-way-request mode="new">
        <set-url>https://hooks.slack.com/services/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</set-url>
        <set-method>POST</set-method>
        <set-body>@{
                return new JObject(
                        new JProperty("username","APIM Alert"),
                        new JProperty("icon_emoji", ":ghost:"),
                        new JProperty("text", String.Format("{0} {1}\nHost: {2}\n{3} {4}\n User: {5}",
                                                context.Request.Method,
                                                context.Request.Url.Path + context.Request.Url.QueryString,
                                                context.Request.Url.Host,
                                                context.Response.StatusCode,
                                                context.Response.StatusReason,
                                                context.User.Email
                                                ))
                        ).ToString();
            }</set-body>
      </send-one-way-request>
    </when>
</choose>
相关策略
相关内容
有关使用策略的详细信息,请参阅:
- 教程:转换和保护 API
- 策略参考,其中提供了策略语句及其设置的完整列表
- 策略表达式
- 设置或编辑策略
- 重复使用策略配置
- 策略片段存储库
- 策略场存储库
- Azure API 管理策略工具包
- 获取 Copilot 帮助以创建、解释策略和排查策略问题