Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
Sam Donato Avatar asked Oct 30 '16 00:10

Sam Donato


People also ask

Does API gateway support XML?

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.

Can Lambda return XML?

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.

Which feature of the API gateway can be used to format the data response?

API Gateway uses Velocity Template Language (VTL) engine to process body mapping templates for the integration request and integration response.

What is content type in API gateway?

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.


1 Answers

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.

like image 129
MikeD at AWS Avatar answered Sep 26 '22 02:09

MikeD at AWS