apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.58k stars 2.53k forks source link

The body-transformer plugin cannot convert the json to xml in the requestBody #11287

Open guipengfei opened 6 months ago

guipengfei commented 6 months ago

Description

A simple SOAP proxy, Here is my route configuration is working properly

{ "uri": "/platform/wsdl/test", "name": "platform-wsdl-test", "methods": [ "POST" ], "plugins": { "body-transformer": { "request": { "template": "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://www.omg.org/IDL-WSDL/1.0/\"><soapenv:Header/><soapenv:Body><ns:test><username>{{username}}</username><age>{{age}}</age></ns:test></soapenv:Body></soapenv:Envelope>" }, "response": { "input_format": "xml", "template": "{% if Envelope.Body.Fault == nil then %}{\"code\":\"SUCCESS\",\"message\":null,\"data\":{*_escape_json(Envelope.Body.testResponse._return)*}}{% else %}{\"code\":\"{{_ctx.var.status}}\",\"message\":\"{{Envelope.Body.Fault.faultstring}}\",\"faultDetail\":{*_escape_json(Envelope.Body.Fault)*}}{% end %}" } }, "proxy-rewrite": { "headers": { "set": { "Accept-Encoding": "identity", "Content-Type": "text/xml;charset=utf-8", "SOAPAction": "TestServiceManager#test" } }, "regex_uri": [ "/platform/wsdl/test", "/TestServiceManager/SN" ] }, "response-rewrite": { "headers": { "set": { "Content-Type": "application/json" } } } }, "upstream_id": "514896707306128334", "status": 1 }

1. The request template is too fixed and needs to specify each parameter, the original request body is a json, How to convert json to xml in one go ? Similar to the response body, I can use the _escape_json() method to convert xml to json. _escape_xml() method does not do this, how to achieve this ?

2. Can I get the request header parameters in the request.template and response.template ? This way, I can get the SOAPAction parameter of the header dynamically, and the method name in the template is not fixed

Environment

nfrankel commented 6 months ago

Your solution is to create a custom plugin to to the transformation from XML to JSON and back again.

Alternatively, you can use API7 that has such a feature out-of-the-box.

Disclaimer: I work for API7