0
votes

In APIM policy, I wish to copy value from request body in inbound policy to response body in outbound policy.

Is there a concept of global variable in APIM policies?

1

1 Answers

0
votes

You can achieve this by using a variable.

Policy:

        <policies>
        <inbound>
            <base />
            <set-variable name="requestBody" value="@(context.Request.Body.As<JObject>(true))" />
        </inbound>
        <backend>
            <!-- Unused for this sample 
            <base />
            -->
        </backend>
        <outbound>
            <base />
            <set-body template="none">@{
                // var responseBodyFromBackend = ((IResponse)context.Response).Body.As<string>(true); 
                var requestBody = context.Variables.GetValueOrDefault<JObject>("requestBody");     
                var response = new JObject();
                response["OriginalRequestBody"] = requestBody;
                return response.ToString();        
            }</set-body>
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>

Request Body:

{
   "hello": "world"
}

Response Body:

{
    "OriginalRequestBody": {
        "hello": "world"
    }
}