1

I have APIM policy where I am using following policy

    <choose>
        <when condition="@(!(context.Variables.GetValueOrDefault<string>("Value").Contains("admin")))">
            <return-response>
                <set-status code="403" reason="Forbidden" />
            </return-response>
        </when>
    </choose>

When policy parsing at run it throw error

"details": "Unable to convert context variable Value of type Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.PipelineResponse to the type of System.String.\r\n at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.Context.ExpressionSyntaxExtensions.GetValueOrDefault[T](IReadOnlyDictionary`2 dictionary, String key, T defaultValue)"

pankaj singh
  • 95
  • 10
  • Does this answer your question? [API Managment unable to cast response body as string](https://stackoverflow.com/questions/42505746/api-managment-unable-to-cast-response-body-as-string) – Ecstasy May 11 '22 at 04:55
  • [APIM Policy - Unable to cast object](https://stackoverflow.com/questions/68480191/apim-policy-unable-to-cast-object) – Ecstasy May 11 '22 at 04:58

1 Answers1

3

I have found the solution for this post. I updated policy from following syntax

       <choose>
        <when condition="@(!((IResponse)context.Variables["Value"]).Body.As<JObject>(preserveContent: true).ToString().Contains("admin"))">
            <return-response>
                <set-status code="403" reason="Forbidden" />
            </return-response>
        </when>
       </choose>
pankaj singh
  • 95
  • 10