Hi,
I have this code running fine except when the request omits the "accountEnabled" attribute
this parameter (accountEnabled) is optional. If it is submitted, it must be true, otherwise the request must be rejected
I did it like this:
<inbound>
<set-variable name="IsAccounPresent" value="@(context.Request.Body.As<JObject>(true).ContainsKey("accountEnabled"))" />
<set-variable name="IsAccountDisabling" value="@(context.Request.Body.As<JObject>(true)["accountEnabled"].Value<bool>() == false)" />
<choose>
<when condition="@(((bool)context.Variables["IsAccounPresent"]) && ((bool)context.Variables["IsAccountDisabling"]))">
<return-response>
<set-status code="401" reason="The attribute 'accountEnabled' can only be set to true!" />
</return-response>
</when>
</choose>
<set-body template="liquid">
{
....other attributes removed for clarity
{%if body.accountEnabled %} ,"accountEnabled":"{<!-- -->{body.accountEnabled}}" {% endif %}
{%if body.email %} ,"email":"{<!-- -->{body.email}}" {% endif %}
}
</set-body>
<base />
</inbound>
the problem is when the optional attribute (accountEnabled) is not submitted I get this exception:
"expression": "context.Request.Body.As<JObject>(true)[\"accountEnabled\"].Value<bool>() == false",
"details": "Value cannot be null.\r\nParameter name: value\r\n at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)"
If I use this below expression, there is no exception but I do not get the correct value for the condition result
<when condition="@((bool)(context.Request.MatchedParameters.ContainsKey("accountEnabled") && ((bool)(context.Request.Body.As<JObject>(true)["accountEnabled"].Value<bool>() == false))))">
Any suggestion?
Thanks,
JD