你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

发送请求

适用于:所有 API 管理层级

send-request 策略将提供的请求发送至指定 URL,等待时间不超过设置的超时值。

注意

按照策略声明中提供的顺序设置策略的元素和子元素。 详细了解如何设置或编辑 API 管理策略

策略语句

<send-request mode="new | copy" response-variable-name="" timeout="60 sec" ignore-error
="false | true">
  <set-url>request URL</set-url>
  <set-method>...</set-method>
  <set-header>...</set-header>
  <set-body>...</set-body>
  <authentication-certificate thumbprint="thumbprint" />
  <proxy>...</proxy>
</send-request>

属性

属性 说明 需要 默认
mode 确定这是是 new 请求还是当前请求中标头和正文的 copy。 在出站策略部分中,mode=copy 不会初始化请求正文。 允许使用策略表达式。 new
response-variable-name 将收到响应对象的上下文变量的名称。 如果该变量不存在,则将在成功执行策略时创建该变量,并且可通过 context.Variable 集合访问该变量。 允许使用策略表达式。 空值
timeout 以秒为单位的超时间隔,此时间过后对 URL 的调用会失败。 允许使用策略表达式。 60
ignore-error 如果为 true 且请求导致错误,则该错误会被忽略,并且响应变量将包含 null 值。 不允许使用策略表达式。 false

元素

元素 说明 必需
set-url 请求的 URL。 允许使用策略表达式。 如果为 mode=copy,则为否;否则为是。
set-method 设置请求的方法。 不允许使用策略表达式。 如果为 mode=copy,则为否;否则为是。
set-header 设置请求中的标头。 将多个 set-header 元素用于多个请求标头。
set-body 设置请求的正文。
authentication-certificate 用于客户端身份验证的证书,在 thumbprint 属性中指定。
proxy 通过 HTTP 代理路由请求。

使用情况

  • 策略节:入站、出站、后端、错误时
  • 策略范围:全局、工作区、产品、API、操作
  • 网关:专用、消耗、自承载

使用注意事项

如果在内部模式下的 VNet 中部署 (注入) 了 API 管理实例,并且使用此策略将 API 请求发送到在同一 API 管理实例中公开的 API,则可能会发生超时,显示 HTTP 500 BackendConnectionFailure 错误。 这是 Azure 负载均衡器限制的结果。

若要在此方案中将 API 请求链接到网关,请将 set-url 配置为使用 localhost 环回 URL https://127.0.0.1。 此外,设置 HOST 标头以指定此 API 管理实例的网关主机。 可以使用默认 azure-api.net 或自定义域主机。 例如:

<send-request>
     <set-url>https://127.0.0.1/myapi/myoperation</set-url>
     <set-header name="Host">
         <value>myapim.azure-api.net</value>
     </set-header>
</send-request>

有关详细信息,请参阅此 博客文章

示例

以下示例演示了如何通过授权服务器验证引用令牌。 有关此示例的详细信息,请参阅通过 Azure API 管理服务使用外部服务

<inbound>
  <!-- Extract token from Authorization header parameter -->
  <set-variable name="token" value="@(context.Request.Headers.GetValueOrDefault("Authorization","scheme param").Split(' ').Last())" />

  <!-- Send request to Token Server to validate token (see RFC 7662) -->
  <send-request mode="new" response-variable-name="tokenstate" timeout="20" ignore-error="true">
    <set-url>https://microsoft-apiappec990ad4c76641c6aea22f566efc5a4e.azurewebsites.net/introspection</set-url>
    <set-method>POST</set-method>
    <set-header name="Authorization" exists-action="override">
      <value>basic dXNlcm5hbWU6cGFzc3dvcmQ=</value>
    </set-header>
    <set-header name="Content-Type" exists-action="override">
      <value>application/x-www-form-urlencoded</value>
    </set-header>
    <set-body>@($"token={(string)context.Variables["token"]}")</set-body>
  </send-request>

  <choose>
        <!-- Check active property in response -->
        <when condition="@((bool)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["active"] == false)">
            <!-- Return 401 Unauthorized with http-problem payload -->
            <return-response>
                <set-status code="401" reason="Unauthorized" />
                <set-header name="WWW-Authenticate" exists-action="override">
                    <value>Bearer error="invalid_token"</value>
                </set-header>
            </return-response>
        </when>
    </choose>
  <base />
</inbound>

有关使用策略的详细信息,请参阅: