Home > Net >  azure apim import policy - Too many characters in character literal
azure apim import policy - Too many characters in character literal

Time:02-01

I'm trying to import a policy to azure api management using a bicep template as follows:

resource allOpsPolicy 'Microsoft.ApiManagement/service/apis/policies@2021-12-01-preview' = {
  name: 'policy'
  parent: apimapi_v1
  properties: {
    value: loadTextContent('all-ops-policy.xml')
    format: 'xml'
  }
}

The content of the all-ops-policy.xml file is as follows:

<policies>
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Request.OriginalUrl.ToString().EndsWith("Public") == false)">
                <rewrite-uri template="@(String.Concat(context.Request.OriginalUrl.ToString(),"/", "MapServer"))" copy-unmatched-params="false" />
            </when>
            <otherwise />
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

When run I get the following error:

One or more fields contain incorrect values: (Code: ValidationError)

  • 'Public' is an unexpected token. Expecting white space.

Can anyone see where I'm going wrong?

CodePudding user response:

The problem was the value of the format property of the allOpsPolicy resource.

I changed from

format: 'xml'

to

format: 'rawxml'

then it worked!

Thanks to Nisha for solving this one

CodePudding user response:

Please replace the single quotes ` with double quotes ".

context.Request.OriginalUrl.ToString().EndsWith('Public') == false)
context.Request.OriginalUrl.ToString().EndsWith("Public") == false

It seems there's also one single quote too much:

String.Concat(context.Request.OriginalUrl.ToString(),'/'', 'MapServer')
String.Concat(context.Request.OriginalUrl.ToString(),"/", "MapServer")

Complete policy:

<policies>
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Request.OriginalUrl.ToString().EndsWith("Public") == false)">
                <rewrite-uri template="@(String.Concat(context.Request.OriginalUrl.ToString(),"/", "MapServer"))" copy-unmatched-params="false" />
            </when>
            <otherwise />
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Issue is not reproducable:
enter image description here

  • Related