Azure APIM Redirect root to a backend based on header setting

SvenGlöckner 436 Reputation points
2023-04-06T12:54:49.9033333+00:00

Hi, is it possible to redirect root traffic to an API backend ? Example: We have an API that's available at /test1. When a call to our root APIM domain is made on https://our-apim.azure-api.net it should forward/redirect to /test1 backend based on a header condition, i.e. Header "Backend" with value "test1". I already tried with the error handler policy <on-error> but had no success. I'm using the base policy that's under "All APIs"

Example:

<choose>
            <when condition="@(context.LastError.Reason == "OperationNotFound" && context.Request.Url.QueryString == "")">
                <set-backend-service backend-id="test-1" />
                <return-response>
                    <set-status code="303" />
                    <set-header name="Location" exists-action="override">
                        <value>https://our-apim.azure-api.net/test1</value>
                    </set-header>
                </return-response>
            </when>
            <otherwise />
        </choose>

this results in an endless redirect loop.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,809 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,236 Reputation points
    2023-04-12T15:52:31.1966667+00:00

    SvenGlöckner Thanks for sharing the additional context. You can define an operation with URL as /test1 with needed policy, backend API, configurations etc. Then add a new operation with URL as / and use send-request policy to call /test1 and then return the response using return-response policy. This way default URL will call operation / and executes the policy defined within the operation.

    actual operation (defines /test1) User's image

    sample operation (defines /) User's image

    Policy Snippet:

    <send-request mode="new" response-variable-name="redirectResponse" timeout="60" ignore-error="true">
                <set-url>https://127.0.0.1/test1</set-url>
                <set-method>GET</set-method>
                <set-header name="Host" exists-action="override">
                    <value><apim-name>.azure-api.net</value>
                </set-header>
                <set-header name="Ocp-Apim-Subscription-Key" exists-action="override">
                    <value>@(context.Request.Headers.GetValueOrDefault("Ocp-Apim-Subscription-Key", ""))</value>
                </set-header>
            </send-request>
            <return-response response-variable-name="redirectResponse" />
    

    Note, the above policy is just for reference based on https://github.com/Azure/api-management-policy-snippets/blob/master/examples/Loopback%20request%20for%20service%20at%20same%20API%20Management%20service.xml and you can customize it based on your need such as checking additional headers etc.

    I hope this helps with your questions and let me know if you have any other.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.