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

Hi Team
Iam facing a similar where my content is like this


Iam sending a response to soap api server using java

My source code looks like this
ā€“In sbb iam building an xml to send the status
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(sbb.toString())));

		OutputFormat format = new OutputFormat(doc);
		format.setIndenting(true);
		format.setIndent(2);
		format.setOmitXMLDeclaration(false);
		format.setLineWidth(Integer.MAX_VALUE);
		Writer outxml = new StringWriter();
		XMLSerializer serializer = new XMLSerializer(outxml, format);
		serializer.serialize(doc);

		response = outxml.toString();
		System.out.println(response);
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return response;
	
}


If you could help that would be great
Thanks for the help in Advance

@digambarcglia

This should be its own question, however it seems like your question is related to the API code which is probably better on a Java forum or StackOverflow.