I am trying to use Amazon's AWS API gateway to front legacy SOAP services with REST. I am able to take a resource request and basically hard code SOAP request in a Body Mapping Template. The SOAP service is called and the XML SOAP response is returned. So far so good.
In the Integration Response, I need to take this SOAP envelope (Basically just XML) and map that back to the JSON model. I don't see how this can be done, but I must be missing something. The following code will get the raw response, but I don't see any way to access the elements:
#set($inputRoot = $input.path('$'))
{
$input.body
}
Imagine my response looks like this:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getProdResponse xmlns:ns1="urn:productlist">
<code xsi:type="xsd:string">100</return>
<message xsi:type="xsd:string">this is a book</return>
</ns1:getProdResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Then in a Body Mapping Template, I want to do something like this:
#set($inputRoot = $input.path('$'))
{
"response-code" : "$input.Envelope.Body.getProdResponse.code",
"response-message" : "$input.Envelope.Body.getProdResponse.message"
}
I know I can probably write a Lambda function to call the SOAP service, but that just doesn't seem reasonable. Am I out of luck?
We quickly integrated the backend SOAP service from Gateway. But then, we realized some great limitations of AWS API GW for our use case: It does not support XML to JSON transformation. It does not parse XML content to modify it, though velocity scripting language provides a great deal of flexibility to modifying JSON.
By default API Gateway and Lambda expect JSON data. It is definitely possible to return XML data, but depending on how you've configured your Lambda integration, it will require different configuration.
API Gateway uses Velocity Template Language (VTL) engine to process body mapping templates for the integration request and integration response.
API Gateway handles all content types in this list as binary. For example, to send a JPEG file using an <img> element in a browser, the browser might send Accept:image/webp,image/*,*/*;q=0.8 in a request.
API Gateway does not currently support direct transformation of XML response bodies. This is a commonly requested feature and is on our backlog, so it will likely get implemented eventually. In the meantime, the only option is to us a Lambda function to call your SOAP back end, parse the response, and return JSON to API Gateway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With