API policy deployment using powershell fails policy error manual copy in portal succeeds

Sridhar Ratna 10 Reputation points
2023-05-27T03:09:10.19+00:00

Hi All,

Below is the full policy file and when i copy this manually in portal and tested, everything works great and no error.

<policies>
    <inbound>
        <base />
        <send-request mode="new" response-variable-name="backendToken" timeout="20" ignore-error="true">
            <set-url>@($"https://{context.Request.MatchedParameters["country"]}{{app-env}}{{app-token}}client_id={{client-id}}&amp;client_secret={{client-secret}}&amp;grant_type={{grant-type}}")</set-url>
            <set-method>GET</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/x-www-form-urlencoded</value>
            </set-header>
        </send-request>
        <choose>
            <when condition="@(((IResponse)context.Variables["backendToken"]).StatusCode == 200)">
                <set-variable name="tokenValue" value="@((((IResponse)context.Variables["backendToken"]).Body.As<JObject>(preserveContent: true)["data"])["Token"].ToString())" />
            </when>
            <otherwise>
                <return-response>
                    <set-status code="500" reason="server error" />
                    <set-body>@(((IResponse)context.Variables["backendToken"]).Body.As<JObject>(preserveContent: true).ToString())</set-body>
                </return-response>
            </otherwise>
        </choose>
        <rewrite-uri template="@($"getProjectDetails/projectId={context.Request.MatchedParameters["projectId"]}&amp;countryName={context.Request.MatchedParameters["country"]}&amp;env={{specmemo-env}}")" />
        <set-backend-service base-url="{{backend-url}}" />
        <set-header name="Authorization" exists-action="override">
            <value>context.Variables.GetValueOrDefault("tokenValue")</value>
        </set-header>
        <set-header name="Authorization" exists-action="override">
            <value>{{specmemo-backend-auth}}</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

I get problem when automating with powershell

PS C:\Users\ratna.reddy> Set-AzApiManagementPolicy -Context $context -ApiId $API_OnAPIM_Id -OperationId $operation.OperationId -Policy $PolicyString
Set-AzApiManagementPolicy :
Error Code: ValidationError
Error Message: One or more fields contain incorrect values:
Request Id: f535612a-bcc3-46fc-9eb3-4999a55e727e
Error Details:
        [Code= ValidationError, Message= 'backendToken' is an unexpected token. Expecting white space. Line 24, position 63., Target= representation]
At line:1 char:1
+ Set-AzApiManagementPolicy -Context $context -ApiId $API_OnAPIM_Id -Op ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (Microsoft.Azure...ExecuteCmdlet():ErrorResponseException) [Set-AzApiManagementPolicy], Exception
    + FullyQualifiedErrorId : Operation returned an invalid status code 'BadRequest',Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.SetAzureApiManagementPolicy

If I remove everything upto "rewrite-url", then it's complaining the same for "getProjectDetails" token unexpected.

Someone please help me on what's the issue here?

Thanks in advance.

Sridhar

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,941 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,310 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sridhar Ratna 10 Reputation points
    2023-05-27T03:33:10.6233333+00:00

    Fixed the issue by passing -Format option and passing policy file rather content

    Set-AzApiManagementPolicy -Context $context -ApiId $API_OnAPIM_Id -OperationId $operation.OperationId -PolicyFilePath $policyFilePath -Format "application/vnd.ms-azure-apim.policy.raw+xml"
    
    
    2 people found this answer helpful.
    0 comments No comments