How to turn a SOAP Service into a REST API

This configuration demonstrates how Membrane can be utilized to transform a SOAP service into a REST API.

Ingredients

Configuration

SOAP messages require the use of POST, so we set the method using groovy. Through template we create the request body containing the SOAP message with a placeholder for the city name parameter from the REST request and send it to the SOAP service at https://www.predic8.de/city-service. The response is converted from XML to JSON for further processing, mapping the SOAP response fields country and population to JSON keys. This setup allows clients to interact with the SOAP service using a RESTful interface, avoiding the need to handle SOAP directly.

<api port="2000">
  <request>
    <groovy>
      exc.request.setMethod("POST")
    </groovy>
    <template contentType="application/xml">
      <![CDATA[
        <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="https://predic8.de/cities">
          <s:Body>
            <c:getCity>
              <name>${params.name}</name>
            </c:getCity>
          </s:Body>
        </s:Envelope>
      ]]>
    </template>
  </request>
  <response>
    <template contentType="application/json">
      {
        "country": "${json["s:Envelope"]["s:Body"]["cs:getCityResponse"]["country"]}",
        "population": "${json["s:Envelope"]["s:Body"]["cs:getCityResponse"]["population"]}"
      }
    </template>
    <xml2Json />
  </response>
  <target url="https://www.predic8.de/city-service" />
</api>
curl localhost:2000?name=Bonn

{
    "country": "Germany",
    "population": "327000"
}

Resources

groovy Examples Documentation
template Examples Documentation
xml2Json Examples Documentation
city-service WSDL