APIM Cors OPTIONS request 500 error

Ivo Bathke 0 Reputation points
2025-08-21T12:50:43.7966667+00:00

Hi,
we have an API Proxy in APIM and have added a CORS policy to allow wildcard subdomains.
The POST requests are working, even with the CORS headers.
However the OPTIONS preflight request fails with a 500 error with no further information.

{

"statusCode": 500,

"message": "Internal server error",

"activityId": "473be759-ecde-4f6b-a49c-07c45daca1dd"

}

Debug tracing does also not work. There is no trace-id returned when the request , even with a valid Apim-Debug-Authorization header.

  1. Why do we get this error on OPTIONS preflight?
  2. Where do I get more information about the cause of the error?
  3. Is there a more easy way for wildcard subdomains in the allowed-origins?

The policy:

    <inbound>
        <!-- Extract origin from request -->
        <set-variable name="requestOrigin" value="@(context.Request.Headers.GetValueOrDefault("Origin", string.Empty))" />
        <!-- Check if origin is allowed -->
        <choose>
            <when condition="@{
            var origin = context.Variables.GetValueOrDefault<string>("requestOrigin");
            // List of allowed exact origins
            var allowedOrigins = new[] {
                "http://localhost:3000",
                "https://production",
                "https://dev"
            };

            return allowedOrigins.Contains(origin) || origin.EndsWith(".staging.de");
            }">
                <set-variable name="corsOrigin" value="@((string)context.Variables["requestOrigin"])" />
            </when>
            <otherwise>
                <set-variable name="corsOrigin" value="" />
            </otherwise>
        </choose>
        <cors allow-credentials="false">
            <allowed-origins>
                <origin>@((string)context.Variables["corsOrigin"])</origin>
            </allowed-origins>
            <allowed-methods preflight-result-max-age="10">
                <method>POST</method>
                <method>GET</method>
            </allowed-methods>
            <allowed-headers>
                <header>*</header>
            </allowed-headers>
        </cors>
        <base />
        <!-- Call backend -->
        <set-backend-service base-url="https://third-party-api/" />
    </inbound>
    <outbound>
        <base />
        <set-header name="Access-Control-Allow-Origin" exists-action="override">
            <value>@((string)context.Variables["corsOrigin"])</value>
        </set-header>
        <set-header name="Vary" exists-action="append">
            <value>Origin</value>
        </set-header>
    </outbound>
    <!-- Control if and how the requests are forwarded to services  -->
    <backend>
        <base />
    </backend>
    <!-- Handle exceptions and customize error responses  -->
    <on-error>
        <base />
    </on-error>
</policies>
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.