API Translation: converting XML responses to REST
When building web applications you don't always control 100% of the source content. Sometimes you are providing middleware for services implemented by other entities. Sometimes you must get content from services and can't control the content type of the responses. You may want to to convert content you get from a service to a content type your application is better able to consume, rather than extending your application to consume the original content type.
Lets assume your application needs to implement the client/consumer side of an existing XML service, but you want your application to consume the content as JSON. In this case we can use the NPM module xml2js which conveniently performs this presentation format conversion for us. We can add the module to our LineRate system like so:
# scripting npm install xml2js
Once installed we have the code that can convert an HTTP response body, we just need a way to fix the response Content-Type header. Assuming you have a Virtual-Server or Forward-Proxy on your LineRate system called 'xml2json_gateway', the following example will convert your XML responses to JSON: