Following is the JSON request body from which i need to read values-
{
"User Id":"123","Name":"Raj","Employee Id":"3425667","Skill Set":"Java Developer"}
Following is the policy code which was working earlier but when i tried today its giving me error-
<policies>
<inbound>
<base />
<set-variable name="mode" value="@(context.Request.OriginalUrl.Query.GetValueOrDefault("mode"))" />
<choose>
<when condition="@(context.Variables.GetValueOrDefault<string>("mode") == "dev")">
<return-response>
<set-status code="200" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"StatusCode": "200",
"Success Message": "Redirected to backend services."
}</set-body>
</return-response>
</when>
<otherwise />
</choose>
</inbound>
<backend />
<outbound>
<base />
<choose>
<when condition="@(string.IsNullOrEmpty(context.Request.Body.As<JObject>(true)["User Id"].Value<string>()))">
<return-response>
<set-status code="404" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"statusCode": "404",
"Error Message" : "Missing User Id of Employee"
}</set-body>
</return-response>
</when>
</choose>
<choose>
<when condition="@(string.IsNullOrEmpty(context.Request.Body.As<JObject>(true)["Name"].Value<string>()))">
<return-response>
<set-status code="404" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"statusCode": "404",
"Error Message" : "Missing Name of Employee"
}</set-body>
</return-response>
</when>
</choose>
<choose>
<when condition="@(string.IsNullOrEmpty(context.Request.Body.As<JObject>(true)["Employee Id"].Value<string>()))">
<return-response>
<set-status code="404" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"statusCode": "404",
"Error Message" : "Missing Employee Id of Employee"
}</set-body>
</return-response>
</when>
</choose>
<choose>
<when condition="@(string.IsNullOrEmpty(context.Request.Body.As<JObject>(true)["Skill Set"].Value<string>()))">
<return-response>
<set-status code="404" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"statusCode": "404",
"Error Message" : "Missing Skill Details of Employee"
}</set-body>
</return-response>
</when>
<otherwise>
<return-response>
<set-status code="200" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>{
"statusCode": "200",
"Message" : "Details saved successfully"
}</set-body>
</return-response>
</otherwise>
</choose>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Following is the error that i am receiving since yesterday-
choose (0.535 ms)
{
"messages": [
{
"message": "Expression evaluation failed.",
"expression": "string.IsNullOrEmpty(context.Request.Body.As<JObject>(true)[\"User Id\"].Value<string>())",
"details": "The message body is not a valid JSON. Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 103.\r\n at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)\r\n at Gateway.Pipeline.IO.JObjectFormatter.Format(JsonTextReader jsonTextReader)\r\n at Gateway.Pipeline.IO.JsonConverter.Convert[T](Stream stream, Encoding encoding, ILog log, Object conversionSettings)\r\n at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.As[T](Boolean preserveContent)"
},
"Expression evaluation failed. The message body is not a valid JSON. Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 103.\r\n at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)\r\n at Gateway.Pipeline.IO.JObjectFormatter.Format(JsonTextReader jsonTextReader)\r\n at Gateway.Pipeline.IO.JsonConverter.Convert[T](Stream stream, Encoding encoding, ILog log, Object conversionSettings)\r\n at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.As[T](Boolean preserveContent)",
"Error reading JObject from JsonReader. Current JsonReader item is not an object: String. Path '', line 1, position 103."
]
}