DEV Community

Cover image for API Management - Convert SOAP POST to GET
Markus Meyer
Markus Meyer

Posted on

4 2

API Management - Convert SOAP POST to GET

API Management is receiving a SOAP POST request with XML in the request body.
Data from the request body has to be forwarded to a GET operation

Import SOAP Service

The public soap service
https://www.dataaccess.com/webservicesserver/numberconversion.wso?op=NumberToDollars is imported in API Management.

image.png

The imported SOAP service in API management:
image.png

Request-Body for SOAP service:

<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope">
  <Body>
    <NumberToDollars xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dataaccess.com/webservicesserver/">
      <dNum>1</dNum>
    </NumberToDollars>
  </Body>
</Envelope>
Enter fullscreen mode Exit fullscreen mode

The target API

An additional operation in the Echo API returns the mocked status 200.

image.png

<policies>
    <inbound>
        <base />
        <mock-response status-code="200" content-type="application/json" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
Enter fullscreen mode Exit fullscreen mode

This target API has to be replaced with a real-world API.

Transform POST to GET

The incoming request body is escaped and also has additional double quotes which has to be removed:

string xml = context.Request.Body.As<string>(preserveContent: true);
xml = Regex.Unescape(xml);

// Remove the double quotes
xml = xml.Remove(0,1);
xml = xml.Remove(xml.Length-1,1);
Enter fullscreen mode Exit fullscreen mode

Transform the XML string into a JSON object:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

var json = JsonConvert.SerializeObject(doc);
var data = JObject.Parse(json );
Enter fullscreen mode Exit fullscreen mode

The transformed JSON string:

{
  "?xml": {
    "@version": "1.0",
    "@encoding": "utf-8"
  },
  "Envelope": {
    "@xmlns": "http://www.w3.org/2003/05/soap-envelope",
    "Body": {
      "NumberToDollars": {
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "@xmlns": "http://www.dataaccess.com/webservicesserver/",
        "dNum": "1"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Finally, the wanted data has to be returned from the JSON Object:

JObject envelope = data["Envelope"] as JObject;
JObject body = envelope["Body"] as JObject;
JObject numberToDollars = body["NumberToDollars"] as JObject;

return numberToDollars["dNum"].Value<string>();

Enter fullscreen mode Exit fullscreen mode

The data has to be forwarded to the mocked API with a GET request:

<set-method>GET</set-method>
<set-backend-service base-url="https://rfqapiservicey27itmeb4cf7q.azure-api.net/echo/200/" />
<rewrite-uri template="@("/test?q=" + context.Variables.GetValueOrDefault<string>("num"))" copy-unmatched-params="false" />

Enter fullscreen mode Exit fullscreen mode

Result

The complete policy:

<policies>
    <inbound>
        <base />
        <set-variable name="num" value="@{
            string xml = context.Request.Body.As<string>(preserveContent: true);
            xml = Regex.Unescape(xml);

            // Remove the double quotes
            xml = xml.Remove(0,1);
            xml = xml.Remove(xml.Length-1,1);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            var data = JObject.Parse(JsonConvert.SerializeObject(doc));
            JObject envelope = data["Envelope"] as JObject;
            JObject body = envelope["Body"] as JObject;
            JObject numberToDollars = body["NumberToDollars"] as JObject;

            return numberToDollars["dNum"].Value<string>();
            }" />
        <set-method>GET</set-method>
        <set-backend-service base-url="https://rfqapiservicey27itmeb4cf7q.azure-api.net/echo/200/" />
        <rewrite-uri template="@("/test?q=" + context.Variables.GetValueOrDefault<string>("num"))" copy-unmatched-params="false" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
Enter fullscreen mode Exit fullscreen mode

Testing it in API management:

image.png

The trace log:

image.png

GitHub answer

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️