API M Policy error : Microsoft.WindowsAzure.ApiManagement.Proxy: The message body is not a valid JSON. Unexpected character encountered while parsing value.

Nasir Khan 26 Reputation points
2022-05-13T20:10:36.293+00:00

<send-request is failed due to JSON parsing error.

Here is what I am trying to achieve.

  1. In my <inbound> policy, I would like to send request to token server to validate token
  2. To validate I use <send-request.
  3. Request is sent to server, howevver in IResponse object I am not getting JSON body.
  4. I am getting error at below line.

<set-variable name="authtoken" value="@((string)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["access_token"])" />

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

...

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.

0 comments No comments

3 answers

Sort by: Newest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  2. Nasir Khan 26 Reputation points
    2022-05-16T16:45:50.52+00:00

    Thanks @JananiRamesh-MSFT . I used your script to send request for generating bearer token, however I am getting below error. Also I think to note, when I use POSTMAN client to get Bearer token, I get it with GET request in POSTMAN not POST.

    Error Details : 401 Unauthorized. Similar to 403 Forbidden, but specifically for use when authentication is possible but has failed or not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource

    Bearer realm="nma3-dpioto-bld-fa2.azurewebsites.net" authorization_uri="https://login.windows.net/{tanentid]/oauth2/v2.0/authorize" resource_id="xxxxx"

    Was this answer helpful?

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    3 deleted comments

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.