set-body policy gives error when saving when body has { } , but still works

Martin Kallukalam 355 Reputation points
2024-09-26T22:23:16.6433333+00:00
<policies>
    <!-- Throttle, authorize, validate, cache, or transform the requests -->
    <inbound>
        <base />
        <choose>
            <when condition="@(1==1)">
                <set-variable name="newaccesstoken" value="TOKENNOTFOUND" />
                <return-response>
                    <set-status code="401" reason="Unauthorized" />
                    <set-body>{"QueryString": "123", "param1": "4567"}</set-body>
                </return-response>
            </when>
            <otherwise>
                <set-variable name="newaccesstoken" value="SOMEOTHERREASON" />
            </otherwise>
        </choose>
    </inbound>
    <!-- Control if and how the requests are forwarded to services  -->
    <backend>
        <base />
    </backend>
    <!-- Customize the responses -->
    <outbound>
        <base />
    </outbound>
    <!-- Handle exceptions and customize error responses  -->
    <on-error>
        <base />
    </on-error>
</policies>

My policy looks like this (never mind the logic of this policy. I wanted to simplify the error I am seeing). In the set-body I am doing a json . While saving the policy it gives me an error that there is a formatting error. see attachment. However I go ahead and click ok to save it. The policy works as expected and I get the json response. What can I do to avoid this error message when saving the policy ?

User's image

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

Accepted answer
  1. JananiRamesh-MSFT 27,341 Reputation points
    2024-09-27T03:42:17.96+00:00

    @Martin Kallukalam Thanks for reaching out. While the policy saves successfully, the warning indicates that the syntax is not fully compliant with the expected format for dynamic expressions.

    To avoid the warning, you should use the correct syntax with the expression enclosed in parentheses, as shown in the corrected policy:

    <set-body>@(new JObject(new JProperty("QueryString", "123"), new JProperty("param1", "4567")).ToString())</set-body>
    

    This ensures that the expression is properly evaluated and avoids any potential issues during runtime.

    do try and let me know incase of further queries, I would be happy to assist you.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Martin Kallukalam 355 Reputation points
    2024-09-27T04:05:36.7033333+00:00

    @JananiRamesh-MSFT It sitll gives me error (different error). I copy pasted exactly as you suggested
    User's image

    0 comments No comments

  2. Martin Kallukalam 355 Reputation points
    2024-09-27T04:06:29.5466667+00:00

    User's image

    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.