how can I manipulate variables in the policy expression azure api management

Claudio Resende 221 Reputation points
2022-10-13T14:12:35.927+00:00

how can I manipulate variables in the policy expression, I want to add/ change a variable value, but the variables is in a immutable map(read-only)

e.g

<policies>  
<inbound>  
<base />  
<set-variable name="body" value="@((context.Request.Body == null) ? "" : (string)context.Request.Body.As<string>(preserveContent: true))" />  
               <set-variable name="errorMessage" value="@("")" />  
            <set-variable name="errorMessage" value="" />  
<choose>  
<when condition="@{  
  
                 //do some stuff  
                var errorMessage = "set an error here ";  
  
              //add the error to the global variable so I can access it on the response.  
                context.Variables["errorMessage"]=errorMessage;  
                      
                      
                return true;  
            }">  
<return-response>  
<set-status code="401" reason="Unauthorized" />  
<set-body>@{  
                    return new JObject(  
                    new JProperty("title", "any error"),  
                    new JProperty("status", "any status"),  
                    new JProperty("detail", (string)context.Variables.GetValueOrDefault<string>("errorMessage"))).ToString();  
                    }</set-body>  
</return-response>  
</when>  
</choose>  
</inbound>  
  
</policies>  
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2022-10-13T17:50:35.46+00:00

    @Claudio Resende Thank you for reaching out to Microsoft Q&A. Based on your statement, you are looking to modify variable value inside the policy expression.

    Unfortunately, that is not possible. Context variable members are read-only as mentioned in docs, and this cannot be changed.

    250127-image.png

    However, using set-variable policy you can set the value of the variable with policy expression like below, but you might have to repeat the logic inside condition as well. Refer Set variable for more info about this policy.

    <set-variable name="errorMessage" value="@{  
                //do some stuff  
                var errorMessage = "set an error here ";  
                return errorMessage;  
            }" />  
            <choose>  
                <when condition="@{  
                    //do some stuff                    
                    return true;  
                    }">  
                    <return-response>  
                        <set-status code="401" reason="Unauthorized" />  
                        <set-body>@{  
                                    return new JObject(  
                                    new JProperty("title", "any error"),  
                                    new JProperty("status", "any status"),  
                                    new JProperty("detail", (string)context.Variables.GetValueOrDefault<string>("errorMessage"))).ToString();  
                                    }</set-body>  
                    </return-response>  
                </when>  
            </choose>  
    

    If you like to share a feedback or suggestion to our product team, feel free to post it in aka.ms/apimwish so that others can upvote it too. I hope this answers your question and feel free to add a comment if you have any other questions. We would be happy to assist you.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.