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