问题
I am fairly new to Postman.
I am trying to generate a file using an XML API and log data to Postman Console while looping through different request parameters ([DateOut] in my case). The file I am trying to generate should look like this:
DateOut | PickupLocationCode | ItemType | Status
10/19/2018 | YVR | A100 | Available
10/20/2018 | YYC | A200 | On Request
I found out how to loop through the data file and get valid responses (in Postman Runner under Response Body). My data loop file (CSV) looks like this:
DateOut | PickupLocationCode
10/19/2018 | YVR
10/20/2018 | YYC
My API request looks like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetItemTypeStatus xmlns="http://someurl.org/">
<Credentials>
<Agent>the_username</Agent>
<Password>the_password</Password>
</Credentials>
<Data>
<PickupLocationCode>{{PickupLocationCode}}</PickupLocationCode>
<DateOut>{{DateOut}}</DateOut>
</Data>
</GetItemTypeStatus>
</soap:Body>
</soap:Envelope>
Here is my API response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetItemTypeStatusResponse xmlns="http://tempuri.org/">
<GetItemTypeStatusResult>
<StatusInfo>
<Success>succeeded</Success>
<Remarks>Available</Remarks>
</StatusInfo>
<ItemsStatus>
<ItemStatus>
<PickupLocationCode>YVR</PickupLocationCode>
<ItemTypeID>109</ItemTypeID>
<ItemType>A100</ItemType>
<Status>Available</Status>
</ItemStatus>
</ItemsStatus>
</GetItemTypeStatusResult>
</GetItemTypeStatusResponse>
</soap:Body>
</soap:Envelope>
Before logging values to console I set the following variables under Pre-request Script:
pm.environment.set("DateOut", "01/01/2018");
pm.environment.set("PickupLocationCode", "YVR");
I am using the test below in Postman to log the values to Console. Source: Extract a value from XML response and set it as a global variable in Postman
// Convert XML output to JSON format
var jsonObject = xml2Json(responseBody);
//Add Departure Date
let departuredate = pm.iterationData.get("DateOut");
//console.log("Departure Date: ", departuredate);
// Take care of Envelope and Body tags, grab data and add DateOut
var activeStatus = jsonObject['soap:Envelope']['soap:Body'].GetItemTypeStatusResponse.GetItemTypeStatusResult.ItemStatus.ItemStatus.concat(departuredate);
// Assigning the extracted value in to a global variable
pm.globals.set ("availability", JSON.stringify(activeStatus));
console.log(['Status '] + pm.globals.get("availability"));
Here is the script that writes the API response to the file. A local server is needed to do this but the set up was very easy. Source: https://documenter.getpostman.com/view/3407886/RWgp1fB5
let dataToFile = {
requestName: request.name || request.url,
fileExtension: 'xml',
responseData: pm.response.text()
};
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);
});
What I have not found out yet is how to write only the values in the tags to the file rather than the entire API response. I would appreciate any help with this.
Thank you very much.
来源:https://stackoverflow.com/questions/52959100/how-to-log-api-responses-to-console-and-save-to-a-data-file-using-pm-sendrequest