azureazure-api-management

Api Management Test Console Fails, Am I Doing something wrong with Inbound Policies?


I'm currently trying to use Api Management to redirect calls to an specific endpoint, so that when someone hits my endpoint https://ApiManagementResource.azure-api.net/auth/ParseState?state=Splited%7cState it gets redirected somewhere else (that other place will be in the State query string)

When I test the endpoint using Google Chrome Browser it works correctly, but whenever I try to test it from the Test Console provided by Azure it fails to execute the task (It doesn't even starts running).

Those are my currently inbound policies

<policies>
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Request.Url.Query.ContainsKey(&quot;state&quot;))">
                <return-response>
                    <set-status code="301" reason="Redirected" />
                    <set-header name="Location" exists-action="override">
                        <value>https://Google.com.ar</value>
                    </set-header>
                </return-response>
            </when>
            <otherwise>
                <return-response>
                    <set-status code="400" reason="Bad Request" />
                    <set-body>Missing or invalid state parameter on Parse State.</set-body>
                </return-response>
            </otherwise>
        </choose>
    </inbound>
    <!-- Control if and how the requests are forwarded to services  -->
    <backend>
        <base />
    </backend>
    <!-- Customize the responses -->
    <outbound>
        <base />
    </outbound>
    <!-- Handle exceptions and customize error responses  -->
    <on-error>
        <base />
    </on-error>
</policies>

I mocked the redirect to avoid problems for now, as this keeps failing.

When testing from Azure Devops site, if I pass the state query parameter it always fails to execute. If not, then I get the 400 response (as expected).

If I erase the following from the inbound policy

<set-header name="Location" exists-action="override">
   <value>https://Google.com.ar</value>
</set-header>

then I can test it perfectly passing the state query string parameter (but of course, it does not redirect me anywhere).

The error I get when it fails is the following pop up

Failed to execute

I don't think I'm doing something wrong because it works from the Browser but it's my first time with Api Management so maybe I am

Any help?


Solution

  • It looks like automatic redirection is not supported in APIM test console. I believe, this happens because test console doesn't handles redirections during testing process unlike browser and postman.

    If you will change the status code to 300 or 304, you will be able to use test console offered by APIM instance but it will only return the response.

    I have changed the policy to as given below-

    <policies>
        <inbound>
            <base />
            <choose>
                <when condition="@(context.Request.Url.Query.ContainsKey(&quot;state&quot;))">
                    <return-response>
                        <set-status code="300" reason="Redirected" />
                        <set-header name="Location" exists-action="override">
                            <value>https://Google.com.ar</value>
                        </set-header>
                    </return-response>
                </when>
                <otherwise>
                    <return-response>
                        <set-status code="400" reason="Bad Request" />
                        <set-body>Missing or invalid state parameter on Parse State.</set-body>
                    </return-response>
                </otherwise>
            </choose>
        </inbound>
    </policies>
    

    enter image description here

    If you want the redirection to work as expected then it is recommended to use either postman or browser for testing purpose.