PostMan:如何将数组/段从一个响应复制到下一个请求?

时间:2018-09-18 08:03:24

标签: json xml api automation postman

第一个帖子在这里! 我一直在从事邮递员自动化工作,并设法复制ID字符串和FAB ID。 我已经看到类似的问题,但并没有特别强调我希望如何做到这一点...

可用性响应;

<SessionInfo FABSessionId="FAB123456789"/>
<VehicleSearchResponse Success="true" ModifiedRequest="false">
    <ViewInfo Offset="0" Length="30"/>
    <ResultSetInfo SortCode="cost" SortAscending="true" MaxItems="900" NumItems="30"/>
    <Vehicles>
        <Vehicle StartDate="20181002" EndDate="20181006" PickUpPoint="XXX" DropOffPoint="XXX">
            <Image URL="https://picture.net/example.jpg"/>
            <MoreInfoTexts>
                <MoreInfo Header="..."/>
                <MoreInfo Header="..."/>
                <MoreInfo Header="..."/>
            </MoreInfoTexts>
            <PickUpDepotDetails Telephone="012345 67890">
                <Address Address1="..." CityOrTown="..." Country="..."/>
            </PickUpDepotDetails>
            <DropOffDepotDetails Telephone="012345 67890">
                <Address Address1="..." CityOrTown="..." Country="..."/>
            </DropOffDepotDetails>
        </Vehicle>
........
 </Vehicles>
</VehicleSearchResponse>

请参见上文...我希望能够从可用性响应到成本核算请求中复制整个“车辆”细分...这是一个数组,因此下面还有20个其他结果.......

强制要求:

<Itinerary EndDate="20181006" ItineraryId="V!XXX!12345!ABC123!XXX!ABC321!XXX" StartDate="20181002">
<Vehicles>
            {{vehicleSetter}}
</Vehicles>
<OptionalExtras>

因此,整个“车辆-/车辆”部分需要复制到{{vehicleSetter}}。

是否有一种方法可以使用类似于(Costing)Pre-RQ脚本的方法;

pm.globals.get("fabSessionSetter");

pm.globals.get("vehicleSetter");

以及(可用)中的某些内容测试脚本之类的

pm.globals.set("vehicleSetter", jsonObject.FAB_VehicleAvailRS.VehicleSearchResponse.Vehicles.Vehicle[2]);

如果您需要更多信息,请告诉我...谢谢!

2 个答案:

答案 0 :(得分:1)

First of all, I would ask devs to use json type data for passing to instead of XML. it's much more convenient, I went this way on the begging of the development of a new project, it brought me many benefits. But in case it's possible in your case. it's not necessary to use global variables.Environment variables are enough.

But try this, I never did it, but perhaps it will help you. https://github.com/cheeriojs/cheerio https://www.w3schools.com/xml/xpath_syntax.asp

答案 1 :(得分:0)

我想我也遇到了类似的问题,必须将json转换为xml,无论哪种方式我都会收到Sivcan Singh的精彩答复

Convert XML to JSON for extracting data then convert JSON back to XML in Postman