<send-request is failed due to JSON parsing error.
Here is what I am trying to achieve.
- In my <inbound> policy, I would like to send request to token server to validate token
- To validate I use <send-request.
- Request is sent to server, howevver in IResponse object I am not getting JSON body.
- I am getting error at below line.
<set-variable name="authtoken" value="@((string)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["access_token"])" />
- Error is : Expression evaluation failed. The message body is not a valid JSON.
Looks like Body which I am passing to <send-request is correct. I verified it on some other policy. It seems (IResponse)context.Variables unable to convert string into JSON. Here are error details.
Plz advise me on any workaround/solution?
Expression evaluation failed. The message body is not a valid JSON. Unexpected character encountered while parsing value: <. Path '', line 0, position 0. at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings) at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.AsJObject(Stream stream, Encoding encoding, JsonSerializerSettings settings) at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.As[T](Boolean preserveContent)
Here is how my policy looks like:
<policies>
<inbound>
<base />
<!-- Send request to Token Server to validate token (see RFC 7662) -->
<send-request mode="new" response-variable-name="tokenstate" timeout="30" ignore-error="true">
<set-url>https:/<my url>/224546038f-3ea4-4f0c-aed1-066eb6544c3b/oauth2/v2.0/token</set-url>
<set-method>GET</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/x-www-form-urlencoded</value>
</set-header>
<set-body>@{
return new JObject(
new JProperty("client_id","57639298-f973-4a40-bd88-00000000"),
new JProperty("client_secret", "hwaaaazzzzzzzzz"),
new JProperty("scope", "hwF7Q~uy-mq.xxxxxxxxxxxx"),
new JProperty("grant_type", "client_credentials")).ToString();
}</set-body>
</send-request>
<set-variable name="authtoken" value="@((string)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["access_token"])" />
<set-header name="Authorization" exists-action="override">
<value>"Bearer " + @((string)context.Variables["authtoken"])</value>
</set-header>
</inbound>
<backend>
<base />
</backend>
<outbound>
...