You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "DebbyMiressa (via GitHub)" <gi...@apache.org> on 2023/03/21 09:13:15 UTC

[GitHub] [apisix-docker] DebbyMiressa commented on issue #443: No body-transformer plugin

DebbyMiressa commented on issue #443:
URL: https://github.com/apache/apisix-docker/issues/443#issuecomment-1477494203

   The Body-Transformer plugin exists in APISIX V3.2.0 if you check the [schema.json](http://localhost:9092/v1/schema) file. There is currently a bug in which the plugin schema is not synchronized with the plugins listed in the [dashboard](http://localhost:9000/plugin/market). Until they push a bug-fix on their next release, maybe try using the command line to enable and configure the plugin. 
   Here is a code snippet that transforms the request body from JSON to XML and transforms the response body from XML to JSON. Before using the code below, change the req_template and res_template to your needs and make sure it applies to your requirements.
   ```sh
   req_template=$(cat <<EOF | awk '{gsub(/"/,"\\\"");};1' | awk '{$1=$1};1' | tr -d '\r\n'
   <?xml version="1.0"?>
   <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Body>
     <ns0:getCountryRequest xmlns:ns0="http://spring.io/guides/gs-producing-web-service">
      <ns0:name>{{_escape_xml(name)}}</ns0:name>
     </ns0:getCountryRequest>
    </soap-env:Body>
   </soap-env:Envelope>
   EOF
   )
   
   rsp_template=$(cat <<EOF | awk '{gsub(/"/,"\\\"");};1' | awk '{$1=$1};1' | tr -d '\r\n'
   {% if Envelope.Body.Fault == nil then %}
   {
      "status":"{{_ctx.var.status}}",
      "currency":"{{Envelope.Body.getCountryResponse.country.currency}}",
      "population":{{Envelope.Body.getCountryResponse.country.population}},
      "capital":"{{Envelope.Body.getCountryResponse.country.capital}}",
      "name":"{{Envelope.Body.getCountryResponse.country.name}}"
   }
   {% else %}
   {
      "message":{*_escape_json(Envelope.Body.Fault.faultstring[1])*},
      "code":"{{Envelope.Body.Fault.faultcode}}"
      {% if Envelope.Body.Fault.faultactor ~= nil then %}
      , "actor":"{{Envelope.Body.Fault.faultactor}}"
      {% end %}
   }
   {% end %}
   EOF
   )
   
   curl http://127.0.0.1:9180/apisix/admin/routes/test_ws \
       -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   {
       "methods": ["POST"],
       "uri": "/ws",
       "plugins": {
           "proxy-rewrite": {
               "headers": {
                   "set": {
                       "Accept-Encoding": "identity",
                       "Content-Type": "text/xml"
                   }
               }
           },
           "response-rewrite": {
               "headers": {
                   "set": {
                       "Content-Type": "application/json"
                   }
               }
           },
           "body-transformer": {
               "request": {
                   "template": "'"$req_template"'"
               },
               "response": {
                   "template": "'"$rsp_template"'"
               }
           }
       },
       "upstream": {
           "type": "roundrobin",
           "nodes": {
               "localhost:8080": 1
           }
       }
   }'
   
   curl -s http://127.0.0.1:9080/ws -H 'content-type: application/json' -X POST -d '{"name": "Spain"}' | jq
   {
     "status": "200",
     "currency": "EUR",
     "population": 46704314,
     "capital": "Madrid",
     "name": "Spain"
   }
   
   # Fault response
   curl -s http://127.0.0.1:9080/ws -H 'content-type: application/json' -X POST -d '{"name": "Spain"}' | jq
   {
     "message": "Your name is required.",
     "code": "SOAP-ENV:Server"
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org