Convert Response Body From Xml To Json In Api Management Policy Expression When Forwarding Response To Different Url
How can I change the response body from XML to Json when forwarding the response to a different URL? I'm specifically forwarding the response to Azure Service Bus. I've tried many
Solution 1:
Here's the solution :) The typical JsonConvert.SerializeXmlNode method isn't allowed in policy expressions.
However good ol' JsonConvert.SerializeObject did the trick.
<send-one-way-request mode="new">
<set-url>http://requestb.in/xje199xj</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
string xml = context.Response.Body.As<string>(preserveContent: true);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return JsonConvert.SerializeObject(doc);
}
</set-body>
</send-one-way-request>
Post a Comment for "Convert Response Body From Xml To Json In Api Management Policy Expression When Forwarding Response To Different Url"