Sending a request with XML data

I am currently trying to send a request in the pre-request script and it requires that I pass XML data. How to I send said data and then send it successfully?

I would try something like this:

const xmlBody = `<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>John Doe</author>
      <title>Postman Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2019-10-01</publish_date>
      <description>An in-depth look at creating APIs 
      with Postman.</description>
   </book>
</catalog>`;

const options = {
  'method': 'POST',
  'url': 'httpbin.org/post',
  'header': {
    'Content-Type': 'application/xml'
  },
  body: xmlBody

}
pm.sendRequest(options, function (error, response) { 
  if (error) throw new Error(error);
  console.log(response.body);
});

Does this help?

A bit, what if I am also adding in a variable that I am referencing from my environment?

This would be an example of the dat I am passing:

<SERVICE __source="172.24.17.191" __ts="1550188134543" md5=" + pm.environment.get(“md5”) + `" request_type=“PMGetMerchantList” session_id=“hoef.184658.756” source_id=“/172.24.16.192:56628” version=“2” />

You can use the new pm.variables.replaceIn function like this:

const xmlBody = `<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>{{price}}</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
</catalog>`;

const options = {
  'method': 'POST',
  'url': 'httpbin.org/post',
  'header': {
    'Content-Type': 'application/xml'
  },
  body: pm.variables.replaceIn(xmlBody)

}


pm.sendRequest(options, function (error, response) { 
  if (error) throw new Error(error);
  console.log(response.body);
});

@vdespa This is the response body I am getting when I run my request in the pre-request script. When I try to convert it to JSON (using xml2Json()), it returns null. Is this still XML or is it something else? If the response body is XML, why is this failing?

Sometimes invalid XML can make the xml2Json function return null.

the issue that you have is because when you use xml2json it will encoding it inside it self and it will be double encoded. therefore you need to actually decode your file.

const convertToJson = (input)=>{
const json = parser.toJson(input);
const decode = JSON.parse(json);
return decode;
}

const result = convertToJson(“your xml file to be converted to JSON”)

console.log(result)

hopefully this helps you

This is great, but if I put this in the pre-request, my test sends two requests – the one from pm.sendRequest, and the second from the postman POST request URL. Is there a way to make the body be part of the request in the URL for the postman request? I mean use the options exactly like you have, but have the “send” be just the postman request.

Yes, you can replace the entire body of the request as I have demonstrated here:

2 Likes

thank you, exactly what I was looking for (great set of videos you have, thank you for that)

1 Like

FYI I’ve added the answers from @vdespa in here:

https://www.postman.com/postman/workspace/postman-answers/collection/9215231-c7c99fbb-b7f9-48a5-8ddc-192e0f8194e4?ctx=documentation

You can then fork it, edit it, and see it work for yourself!

1 Like