Use Azure Api Management as a passthrough

2019-04-09 07:16发布

I would like to create a policy in Azure API Management that forwards all calls that start with the path "proxy/search" to another url. However, i don't want to have to import/create endpoints in APIM for every possibility since this makes it a maintenance nightmare. For example..

to the corresponding...

I've built the policy below but it looks like APIM wants exact routes to map from it to the backend. I don't want to do this because this proxy may be forwarding to many, many routes apis etc...

<policies>
    <inbound>
        <base />
        <set-variable name="baseUrlSearch" value="https://mysearchapi.com/" />
        <set-variable name="matchSearch" value="proxy/search" />
        <set-variable name="isRoutingComplete" value="false" />
        <set-variable name="apiVersionDefaultSearch" value="1.0" />
        <choose>
            <when condition="@{return context.Request.Url.Path.Contains(context.Variables.GetValueOrDefault<string>("matchSearch"));}">
                <set-backend-service base-url="@(context.Variables.GetValueOrDefault<string>("baseUrlSearch"))" />
                <rewrite-uri template="@(context.Request.Url.Path.Replace(context.Variables.GetValueOrDefault<string>("matchSearch"), ""))" />
                <set-header name="Api-Version" exists-action="skip">
                    <value>@(context.Variables.GetValueOrDefault<string>("apiVersionDefaultSearch"))</value>
                </set-header>
                <set-variable name="isRoutingComplete" value="true" />
            </when>
            <when condition="@(!context.Variables.GetValueOrDefault<bool>("isRoutingComplete"))">
                <return-response>
                    <set-status code="400" reason="Bad Request Through Proxy" />
                </return-response>
            </when>
        </choose>
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>

2条回答
三岁会撩人
2楼-- · 2019-04-09 07:44

You are making your life much harder than it needs to be. Simply create an operation that uses /proxy/* as the template and it will match to all the URLs you identified.

Then just create a policy for that operation that does set-backend-service.

查看更多
Evening l夕情丶
3楼-- · 2019-04-09 07:57

Adding to Darrel Miller's answer, here is how i got it working...

Adding an operation...

{
  "name": "Search_GET",
  "method": "GET",
  "urlTemplate": "/search/*",
  "policies": null
}       

Adding a policy for that operation...

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="https://mysearchapi.com/" />
        <rewrite-uri template="@(context.Request.Url.Path.Replace("search/", ""))" />
        <set-header name="Api-Version" exists-action="skip">
            <value>1.0</value>
        </set-header>
    </inbound>
    <outbound>
        <base />
    </outbound>
</policies>
查看更多
登录 后发表回答