skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search