1

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

1

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" } } 
Sign up to request clarification or add additional context in comments.

3 Comments

it gives syntax errors if you use <, > inside value string. Use encoded xml values for symbols as per this link. w3schools.com/xml/xml_syntax.asp
@shehanpathi I'm not able to reproduce your issue. If I copy the entire policy, APIM saves it without any error messages. Did you change anything?
I had to change <JObject> to xml encoded string -> &lt;JObject&gt;. Otherwise it gives me xml parsing errors in VS editor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.