Skip to content

Instantly share code, notes, and snippets.

@ozkary
Last active October 22, 2021 16:04
Show Gist options
  • Save ozkary/816062946ecd2295683bc479ec49bd14 to your computer and use it in GitHub Desktop.
Save ozkary/816062946ecd2295683bc479ec49bd14 to your computer and use it in GitHub Desktop.
API Management transforms a SOAP API into JSON API
<policies>
<inbound>
<base />
<rewrite-uri template="/websamples.countryinfo/CountryInfoService.wso" copy-unmatched-params="false" />
<set-body template="liquid">
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns="http://www.oorsprong.org/websamples.countryinfo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ListOfContinentsByName>
</ListOfContinentsByName>
</soap:Body>
</soap:Envelope>
</set-body>
<set-header name="Content-Type" exists-action="override">
<value>application/soap+xml; Action="ListOfContinentsByName"</value>
</set-header>
<set-method>POST</set-method>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<choose>
<when condition="@(context.Response.StatusCode < 400)">
<set-body template="liquid">
{
"continents":
[
{% JSONArrayFor item in body.envelope.body.ListOfContinentsByNameResponse.ListOfContinentsByNameResult -%}
{
"code": {% if item.sCode %}"{{item.sCode | Replace: '\r', '\r' | Replace: '\n', '\n' | Replace: '([^\\](\\\\)*)"', '$1\"'}}"{% else %} null {% endif %},
"name": {% if item.sName %}"{{item.sName | Replace: '\r', '\r' | Replace: '\n', '\n' | Replace: '([^\\](\\\\)*)"', '$1\"'}}"{% else %} null {% endif %}
}
{% endJSONArrayFor -%}
]
}</set-body>
</when>
<otherwise>
<set-variable name="old-body" value="@(context.Response.Body.As<string>(preserveContent: true))" />
<!-- Error response as per https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md#7102-error-condition-responses -->
<set-body template="liquid">{
"error": {
"code": "{{body.envelope.body.fault.faultcode}}",
"message": "{{body.envelope.body.fault.faultstring}}"
}
}</set-body>
<choose>
<when condition="@(string.IsNullOrEmpty(context.Response.Body.As<JObject>(preserveContent: true)["error"]["code"].ToString()) && string.IsNullOrEmpty(context.Response.Body.As<JObject>(preserveContent: true)["error"]["message"].ToString()))">
<set-body>@{
var newResponseBody = new JObject();
newResponseBody["error"] = new JObject();
newResponseBody["error"]["code"] = "InvalidErrorResponseBody";
if (string.IsNullOrEmpty((string)context.Variables["old-body"]))
{
newResponseBody["error"]["message"] = "The error response body was not a valid SOAP error response. The response body was empty.";
}
else
{
newResponseBody["error"]["message"] = "The error response body was not a valid SOAP error response. The response body was: '" + context.Variables["old-body"] + "'.";
}
return newResponseBody.ToString();
}</set-body>
</when>
</choose>
</otherwise>
</choose>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment