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

vinit sawant 11 Reputation points
2024-01-13T06:14:54.4466667+00:00

Hello , I am facing an issue.

The GET request to the, https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token.

in the APIM Policy <Send-Request> block.

Returns Exception: "Unexpected character encountered while parsing value: <. Path '', line 0, position 0."


My Policy:

<send-request mode="new" response-variable-name="bearerToken" timeout="20" ignore-error="false">
      <set-url>https://login.microsoftonline.com/{{aad-tenant}}/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 "client_id=68b5d0d9-xxxx-xxxx-xxxx-b921efcfad9b&scope=api://xxxxxxx-c2f1-xxxxx-xxxx-08e09cf10ca4/.default&client_secret=xxxxx~IQ.-uN9EAKtyoWPk3Rm6v-xxxx.xxxxxx&grant_type=client_credentials";
            }</set-body>
</send-request>
<set-header name="Authorization" exists-action="override">
    <value>@("Bearer " + (String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"])</value>
</set-header>

Also, in the Docs here suggest POST request for the above URL, but POST request results in a bad request error. A GET request to the same URL results 200 OK.
In POSTMAN, Both POST and GET results in 200 OK.
Not Sure what is wrong. But the Main problem is to extract the access_token from the response body which is why i am racing this issue. Please Assist.

PS: I have read the similar Question posted Here in qna community which is not helping me, So please do not redirect me to look for the solution there. "even posted a comment for help there ",

Many Thanks.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,455 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2024-01-16T19:07:50.22+00:00

    vinit sawant Thanks for posting your question in Microsoft Q&A. As discussed in other Q&A thread, you need to make a POST request to the endpoint in order to get JSON response back. If you make a GET request, the endpoint might not return a JSON response and hence processing the response body as JSON will threw an error as you mentioned.

    I understand POST request threw a bad request error, and suggest you validate client id, scope, secret credentials etc. as I don't see any issue with the code snippet (sample reference). You can also make this POST request from other tools such as Postman to validate the request and response.

    To troubleshoot this in APIM, use Trace a call option to capture a trace and investigate the request sent from APIM to the endpoint and review the full error message including stack trace if any. If so, share the full error details including request ID/correlation ID, timestamp. That will help in investigating further.

    I hope this helps and if you face any issues or questions, feel free to reach out.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    1 person found this answer helpful.
    0 comments No comments

Your answer

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