Sam Donato
Sam Donato

Reputation: 481

How do I convert XML to JSON AWS API Gateway?

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?

Upvotes: 6

Views: 10871

Answers (1)

MikeD at AWS
MikeD at AWS

Reputation: 3745

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.

Upvotes: 9

Related Questions