XML response escape characters

Hi everyone.

I’m trying to obtain a correct XML response without escape characters but seems i’m not capable of.
I’m sending a POST request with an XML, it sends and receives correctly a response (also an XML), but there are escape characters even if I click on Raw response.

I’ve tried to change some header settings (Accept-Charset, Accept, Accept-enconding) to see if allowing UTF-8 makes some difference but it doesn’t.

My request body is as follows:

<?xml version="1.0" encoding="utf-8" ?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sisnet.framework.webservices.jaxws">

   <soapenv:Header/>
   <soapenv:Body>
      <urn:execute>
         <!--Optional:-->
         <arg0>LOGIN</arg0>
         <!--Optional:-->
         <arg1>
         <![CDATA[
            <ROOT>
               <COMANDO>
                  <CODIGO>LOGIN</CODIGO>
                  <VERSION>1</VERSION>
                  <TIPODUSO>VOID</TIPODUSO>
               </COMANDO>
               <ENTRADA>
                  <USUARIO>{{user}}</USUARIO>
                  <CONTRASEÑA>{{pass}}</CONTRASEÑA>
                  <CODIMEDI>{{codMediator}}</CODIMEDI>
                  <PROVACCE>{{accessProv}}</PROVACCE>
               </ENTRADA>
            </ROOT>
        ]]>
         </arg1>
      </urn:execute>

And the obtained response is:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <S:Body>
        <ns2:executeResponse xmlns:ns2="urn:sisnet.framework.webservices.jaxws">
            <return>&lt;?xml version="1.0" encoding="UTF-8" ?>
&lt;ROOT>
&lt;RETORNO>
&lt;CODIGO>0&lt;/CODIGO>
&lt;DESCRIPCION>Comando invocado correctamente.&lt;/DESCRIPCION>
&lt;EXCEPCION>&lt;/EXCEPCION>
&lt;COMENTARIO>&lt;/COMENTARIO>
&lt;AVISOS>
&lt;/AVISOS>

&lt;/RETORNO>
&lt;RESPUESTA>
&lt;IDSESION>WSSISnetesbarint01U6864I16b35a8a0771adec91ea21bdb4960fa12&lt;/IDSESION>

&lt;/RESPUESTA>
&lt;/ROOT></return>
        </ns2:executeResponse>
    </S:Body>
</S:Envelope>

I really apreciate it if you can help me.
Thanks!

Hi @srepetop,

Welcome to the community!

This seems like an odd SOAP WebService, almost like it’s proxying XML payloads instead of representing an origin server. In any case, it looks like you’re doing everything correctly, as you’re getting a valid response. To get a cleaner-looking representation, one thing you can do is use the visualizer feature.

In the Tests tab of your request, include this code:

const template = `
<textarea style="width: 100%; height: 100%; font-family: monospace;" readonly>{{payload}}</textarea>
`;

const response = pm.response.text();
const $ = cheerio.load(response);
const xmlPayload = $('return').text();

pm.visualizer.set(template, {
    payload: xmlPayload
})

Then click the Visualize tab in the response body pane. You should see something like this:

EDIT: For extra credit, you can try to format the XML before displaying it so it’s a bit easier to navigate. That would look something like this:

const xml2js = require('xml2js');

const template = `
<textarea style="width: 100%; height: 100%; font-family: monospace;" readonly>{{payload}}</textarea>
`;

const response = pm.response.text();
const $ = cheerio.load(response);
const xmlPayload = $('return').text();

xml2js.parseString(xmlPayload, (_, res) => {
    const builder = new xml2js.Builder();
    const xml = builder.buildObject(res);

    pm.visualizer.set(template, {
        payload: xml
    });
});

Hope that helps!

Best,

Kevin

Hi again @kevin.swiber !

Thank you so much for your response, it was really helpful!
Now I understand that this is not “my” problem with the config, it is just that the response is odd.

Sadly it means that I cannot navigate through the XML tags as normal, because even if I convert it to a JSON format, the result presents as a whole block, not divided by tags (I’ve tried it).
So what I will do now is play with splits and arrays (ouch).
If you think there is a better way to do what I want please let me know.

Thank you so much again and have a nice day!
btw sorry for my english, I know it is not so good but I’m trying hehe

Hey @srepetop,

Gah! Don’t do splits! The xml2js library should work on the text string. You could use xml2js for both parts here, but I’m using cheerio to get scan through the top-level XML document, because I think it looks a little cleaner.

const xml2js = require('xml2js');

const response = pm.response.text();
const $ = cheerio.load(response);
const xmlPayload = $('return').text();

xml2js.parseString(xmlPayload, (_, res) => {
    console.log(res)
});

This should get you a JSON object you can traverse.

Hope that helps.

Best,

Kevin

OMG @kevin.swiber you are my absolute savior!!
Sorry I didn’t understand you the first time, now it makes much more sense to me.
I’m able now to navigate through the whole XML with any problem :slight_smile:

Thank you so much again, I owe you one!

1 Like

Hi
I am also facing same issue in XML response

I tried by using script which you posted earlier in test tab but facing an error “There was an error in evaluating the test script: TypeError: Cannot read property ‘set’ of undefined”

Script i used in test tab

const template = <textarea style="width: 100%; height: 100%; font-family: monospace;" readonly>{{payload}}</textarea>;

const response = pm.response.text();
const = cheerio.load(response); const xmlPayload = (‘return’).text();

pm.visualizer.set(template, {
payload: xmlPayload
})

Hey @shubhangi.masal,

That’s really strange. What’s the version of your Postman app? It needs to be at least Postman v7.7.2. The latest is v7.29.1.

Best,

Kevin