telefonicaid / perseo-fe

Front End process for the Perseo CEP
GNU Affero General Public License v3.0
14 stars 30 forks source link

Support sending XML payloads with PostAction #763

Closed rg2011 closed 8 months ago

rg2011 commented 8 months ago

I'm trying to use the POST Action to send an HTTP Request with an XML payload, but the payload is getting stringified before being sent (i.e. it gets enclosed in quotes), so it is not valid XML.

This is my action (obfuscating the URL and actual payload):

 {
            "type": "post",
            "parameters": {
                "url": "https://webhook.site/***OFUSCATED***",
                "method": "POST",
                "headers": {
                    "Content-Type": "application/soap+xml; charset=utf-8"
                }
            },
            "template": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ser=\"http://ofuscated\">\n    <soap:Header />\n    <soap:Body>\n${payload}</soap:Body>\n</soap:Envelope>"
        }

The ${payload} inside the template is properly replaced, but the webhook receiver gets the whole message encoded within quotes:

imagen

I believe the problem lies here: https://github.com/telefonicaid/perseo-fe/blob/582b00cb3a14e040163e3771b296710e498ef6fb/lib/models/postAction.js#L61-L64

Calling JSON.stringify on a string returns a quouted string, e.g. JSON.stringify("hello") === '"hello"'. If options.text is already a string, I think it shouldn't be stringified. If the users do want the payload to be quoted, they can use the json option, or add the quotes in the template itself.

mrutid commented 8 months ago

At (private) beaches-vertical: { "type": "post", "template": "<?xml version='1.0' encoding='UTF-8'?>1.0XXXXXXXXXXXXXXXXXXXXXXX${telefono}XXXXXXXXXXXXXAll</soapenv:Body></soapenv:Envelope>", "parameters": { "url": "https://www.mensajerianegocios.movistar.es/XXXXXXXXXXX", "method": "POST", "headers": { "Content-type": "application/soap+xml;charset=UTF-8", "SOAPAction": "smsTextSubmit" } } }

we used a similar rule that worked. So it's weird. I Don't see new adds or regresion code, but I agree in the double quoting problem you state.

The code comment seems to address the case of template "string" or Buffer... so why do we need to stringify? is there any chance to get JSON in the template? what happened in that case? (I suppose JSON should be managed as the XML does) except we were using it as a constant in order to avoid scape quotes.

rg2011 commented 8 months ago

The code comment seems to address the case of template "string" or Buffer... so why do we need to stringify? is there any chance to get JSON in the template? what happened in that case? (I suppose JSON should be managed as the XML does) except we were using it as a constant in order to avoid scape quotes.

I am not familiar with the perseo-fe codebase, but according to the documentation of the template substitution feature (https://github.com/telefonicaid/perseo-fe/blob/master/docs/API/plain_rules.md#actions):

Attribute value of update action and template of post action are expanded to numerical, boolean or JSON stringyfied values instead of string values when is possible. For example, if we have {"a": "${x}"}:

If the value of attribute x is 42 then it will expand do {"a": 42} and not to {"a": "42"} If the value of attribute x is {"hello": "world"} then it will expand to {"a": "{\"hello\":\"world\"}"} (expand to native JSON, i.e. {"a": {"hello": "world"}}, is not supported)

So I guess perseo does recognize some patterns in the template and might try to convert it to primitive types in some cases. In that case, the stringify would make sense.

fgalan commented 8 months ago

PR https://github.com/telefonicaid/perseo-fe/pull/764