Reputation: 93
Note: I am using azure apim
I have two api returning xml response show below: Response1:
<A>
<B>10</B>
</A>
Response2:
<C>
<D>10</D>
</C>
I want to transform XML response to get the below output , where the Response 2 is put inside the Response 1 Output:
<A>
<B>10</B>
<C>
<D>10</D>
</C>
</A>
Upvotes: 3
Views: 962
Reputation: 16076
Set inbound policy to achieve it.
My idea is combining using send-request
,set-variable
and return-response
, that's calling 2 APIs first, and save specific value of the request and combine them into a new Xml document. Here's my policy, I used 2 azure function to play the role of API :
<inbound>
<base />
<send-request mode="new" response-variable-name="reqone" timeout="20" ignore-error="true">
<set-url>https://xxx.azurewebsites.net/api/HttpTrigger1</set-url>
<set-method>GET</set-method>
</send-request>
<send-request mode="new" response-variable-name="reqtwo" timeout="20" ignore-error="true">
<set-url>https://xxxx.azurewebsites.net/api/HttpTrigger2</set-url>
<set-method>GET</set-method>
</send-request>
<set-variable name="valOne" value="@{
string text = ((IResponse)context.Variables["reqone"]).Body.As<XDocument>().Root.Value;
return text;
}" />
<set-variable name="valTwo" value="@{
string text = ((IResponse)context.Variables["reqtwo"]).Body.As<XDocument>().Root.Value;
return text;
}" />
<return-response>
<set-status code="200" />
<set-header name="Content-Type" exists-action="override">
<value>text/xml</value>
</set-header>
<set-body>@{
XDocument srcTree = new XDocument(
new XElement("A",
new XElement("B", context.Variables.GetValueOrDefault("valOne","")),
new XElement("C", new XElement("D", context.Variables.GetValueOrDefault("valTwo","")))
)
);
return srcTree.ToString();
}</set-body>
</return-response>
</inbound>
This is my api response:
This is my test result:
Upvotes: 3