admin管理员组

文章数量:1127072

How to remove a part of uri path in the azure APIM policy ?

Original Request follows a pattern: /***

Desired Url, without /preprod route in it,
ex: ,

<policies>   
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Request.OriginalUrl.Path.Contains("/preprod"))">
                <set-backend-service backend-id="pub-lb-api-v1" />
                <set-header name="Host" exists-action="override">
                    <value>test.abc</value>
                </set-header>
            </when>
   ....
</policies>

How to remove a part of uri path in the azure APIM policy ?

Original Request follows a pattern: https://apim.com/preprod/api/v1/***

Desired Url, without /preprod route in it,
ex: https://test.abc.com/api/v1/products, https://test.abc.com/api/v1/order?xyz=1

<policies>   
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Request.OriginalUrl.Path.Contains("/preprod"))">
                <set-backend-service backend-id="pub-lb-api-v1" />
                <set-header name="Host" exists-action="override">
                    <value>test.abc.com</value>
                </set-header>
            </when>
   ....
</policies>

Share Improve this question edited Jan 8 at 20:05 userahd asked Jan 8 at 19:22 userahduserahd 576 bronze badges 1
  • Is this approach correct ? <when>.... </set-header><find-and-replace from="preprod/api" to="api" /></when> – userahd Commented Jan 8 at 20:09
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the below given policy to remove a part of Uri parameter.

<policies>
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Request.OriginalUrl.Path.Contains("/preprod"))">
                <rewrite-uri template="@((context.Request.OriginalUrl.Path).Replace("/preprod", ""))" />
                <set-backend-service base-url="https://test.abc.com" />
                <set-header name="Host" exists-action="override">
                    <value>test.abc.com</value>
                </set-header>
            </when>
        </choose>
    </inbound>
</policies>

This will remove the preprod from the Uri path and provide you the expected response.

本文标签: url rewritingremove a part of url in api management policiesStack Overflow