我对Postman还是陌生的。
我正在尝试使用XML API生成文件并将数据记录到Postman控制台,同时循环浏览不同的请求参数(在我的情况下为[DateOut])。我尝试生成的文件应如下所示:
DateOut | PickupLocationCode | ItemType |状态
10/19/2018 | YVR | A100 |可用
10/20/2018 | YYC | A200 |根据要求
我发现了如何遍历数据文件并获得有效的响应(在“响应正文”下的Postman Runner中)。我的数据循环文件(CSV)如下所示:
DateOut | PickupLocationCode
10/19/2018 | YVR
2018年10月20日| YYC
我的API请求如下:
<?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>
这是我的API响应:
<?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>
在将值记录到控制台之前,我在“预请求脚本”下设置了以下变量:
pm.environment.set("DateOut", "01/01/2018");
pm.environment.set("PickupLocationCode", "YVR");
我正在使用Postman中的以下测试将值记录到控制台。来源: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"));
这是将API响应写入文件的脚本。需要使用本地服务器来执行此操作,但是设置非常简单。来源: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);
});
我尚未发现的是如何仅将标记中的值写入文件,而不是整个API响应。我将不胜感激。
非常感谢您。