How do I convert XML to JSON AWS API Gateway?

这一生的挚爱 提交于 2020-01-04 05:24:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40325005/how-do-i-convert-xml-to-json-aws-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!