Pingpong
Pingpong

Reputation: 8009

Get specified url part in policy with Azure APIM Policy

Is it possible to, within policy, get the specified url part below:

<policies>
        <inbound>
            <base />    
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
            <find-and-replace from="https://thirdparty/certs" 
                 to="@(specified url part)" />        
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>

Please note that the whole url might include versioning and prefix.

Upvotes: 5

Views: 15377

Answers (2)

Vitaliy Kurokhtin
Vitaliy Kurokhtin

Reputation: 7810

The whole URI (let's assume https://contoso.com/myapi/.wellknown/openid-config) is comprised of:

  1. Scheme: context.Request.OriginalUrl.Scheme
  2. Host: context.Request.OriginalUrl.Host
  3. Optional port: context.Request.OriginalUrl.Port
  4. API suffix: context.Api.Path
  5. Operation path: the rest of it

So operation part of URL can be calculated by something close to: context.Request.OriginalUrl.Path.Trim('/').Substring(context.Api.Path.Trim('/').Length)

Upvotes: 12

DixitArora-MSFT
DixitArora-MSFT

Reputation: 1811

Example of a policy expression, that when used in the inbound path can be used to redirect an incoming request based on version information included as a query parameter in a published API.

<choose>
    <when condition="@(context.Request.Url.Query.GetValueOrDefault("version") == "2014-03")">
      <set-backend-service base-url="http://contoso.com/api/1.6/" />
    </when>
    <otherwise>
      <set-backend-service base-url="http://contoso.com/api/2.0/"
    </otherwise>
  </choose>

Find more details at https://azure.microsoft.com/en-us/blog/policy-expressions-in-azure-api-management/

Upvotes: 0

Related Questions