# Strip XML tags from API response using pm.sendRequest

**URL:** <https://community.postman.com/t/strip-xml-tags-from-api-response-using-pm-sendrequest/3115>\
**Category:** Help Hub\
**Tags:** tests, pmsendrequest\
**Created:** [October 26, 2018, 6:37pm UTC](https://community.postman.com/t/strip-xml-tags-from-api-response-using-pm-sendrequest/3115 "2018-10-26T18:37:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markus.k](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/markus.k/32/1048_2.png) [@markus.k](https://community.postman.com/u/markus.k)\
**Post date:** [October 26, 2018, 6:37pm UTC](https://community.postman.com/t/strip-xml-tags-from-api-response-using-pm-sendrequest/3115/1 "2018-10-26T18:37:16Z")

</div>

Hello,

I am writing API responses to a file using pm.sendRequest. I have set up a local server and use the script below which writes the entire XML response to the file as described here: [[https://documenter.getpostman.com/view/3407886/RWgp1fB5](https://documenter.getpostman.com/view/3407886/RWgp1fB5)]

The issue is that I would like to write **only the values of the XML tags** to the file.

```auto
// Convert XML output to JSON format
var jsonObject = xml2Json(responseBody);

//Add Departure Date and Return Date
let departuredate = pm.iterationData.get("DateOut");
let returndate = pm.iterationData.get("DateIn");

// Take care of Envelope and Body tags, grab data and add DateOut and DateIn
var activeStatus = jsonObject['soap:Envelope']['soap:Body'].GetItemTypeStatusResponse.GetItemTypeStatusResult.ItemsStatus.ItemStatus.concat(departuredate).concat(returndate);

// Assigning the extracted value in to a variable
pm.environment.set ("availability", JSON.stringify(activeStatus));

//Log to Console
console.log(['Status '] + pm.environment.get("availability"));

// Write to file
let dataToFile = {
    requestName: request.name || request.url,
    fileExtension: 'CSV',
    responseData: JSON.stringify(activeStatus) + 'Departure Date: ' + departuredate + ' Return Date: ' + returndate
    };
    
pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(dataToFile)
    }
}, function(err, res) {
    console.log(res);
});

```

Is there a way to do this?

I would appreciate any help with this.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![bhargavkaranam96](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@bhargavkaranam96](https://community.postman.com/u/bhargavkaranam96)\
**Post date:** [October 27, 2018, 11:06am UTC](https://community.postman.com/t/strip-xml-tags-from-api-response-using-pm-sendrequest/3115/2 "2018-10-27T11:06:33Z")

</div>

Hi @markus.k,  
You can use the library `xml2Json` that comes with Postman to convert XML to JSON so that you can extract the values from your XML response. For example,

```javascript
let responseJson = xml2Json(pm.response.text());

```

`responseJson` will contain key value pairs of your tags and values.

---

<div class="post-metadata">

**Author:** ![markus.k](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/markus.k/32/1048_2.png) [@markus.k](https://community.postman.com/u/markus.k)\
**Post date:** [October 29, 2018, 6:01pm UTC](https://community.postman.com/t/strip-xml-tags-from-api-response-using-pm-sendrequest/3115/3 "2018-10-29T18:01:17Z")

</div>

Hi @bhargavkaranam96,  
Thanks very much for your help. I have updated the code accordingly (see above) but the response is [object Object][object Object]. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![markus.k](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/markus.k/32/1048_2.png) [@markus.k](https://community.postman.com/u/markus.k)\
**Post date:** [October 29, 2018, 8:30pm UTC](https://community.postman.com/t/strip-xml-tags-from-api-response-using-pm-sendrequest/3115/4 "2018-10-29T20:30:51Z")

</div>

Hi again @bhargavkaranam96,  
I found out I had to stringify the results. 😀

The script above works now and writes the API responses (tags and values only) and request parameters to a CSV file and also logs the results to Console in Postman.

Thanks again for your help.
