在 Postman 中解析对另一个请求的 XML 响应 [英] Parse XML response to another requests in Postman

查看:49
本文介绍了在 Postman 中解析对另一个请求的 XML 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须测试一些以 XML 格式响应的网络服务,我想解析从第一个请求到第二个请求调用的响应.

I must to test a few webservices who response in XML format and i want to parse the response from first request to second request call.

例如:我提出第一个请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:exec>
         <!--Optional:-->
         <ser:sName>55</ser:sName>
         <!--Zero or more repetitions:-->
         <ser:sArguments>{{Param1}}</ser:sArguments>
         <ser:sArguments>XX</ser:sArguments>
         <ser:sArguments>POSTMAN</ser:sArguments>
         <ser:sArguments></ser:sArguments>
         </ser:exec>
   </soapenv:Body>
</soapenv:Envelope>

谁回复:

<soap:Body>
        <ns2:execResponse xmlns:ns4="http://address.com" xmlns:ns3="http://services" xmlns:ns2="http://services.com">
            <ns2:execReturn>6666&#xd;
</ns2:execReturn>
        </ns2:execResponse>
    </soap:Body>
</soap:Envelope>

我想将 6666 放在 GlobalVariable 或 EnvironmentVariable 中,以便在第二次请求调用中使用.

And i want to put 6666 in a GlobalVariable or EnvironmentVariable to use in second request call.

我到现在为止的尝试:

首先,我在管理环境 - GLOBALS 中设置了一个参数(NumberReq),然后在 TESTS 中输入了以下代码:

First i set a parameter(NumberReq) in Manage Environments - GLOBALS then in TESTS i put this code:

var jsonData = xml2Json(responseBody);
postman.setEnvironmentVariable("NumberReq", jsonData.execReturn);

在下一个请求中,我尝试使用 NumberReq 参数,如下所示:

And in the next request i try to use the NumberReq param like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.com">
       <soapenv:Header/>
       <soapenv:Body>
          <ser:exec>
             <!--Optional:-->
             <ser:sName>99</ser:sName>
             <!--Zero or more repetitions:-->
             <ser:sArguments>00</ser:sArguments>
             <ser:sArguments>{{NumberReq}}</ser:sArguments>
             <ser:sArguments>{{Param2}}</ser:sArguments>
             <ser:sArguments>{{Param3}}</ser:sArguments>
             </ser:exec>
       </soapenv:Body>
    </soapenv:Envelope>

我有这两个网络服务的集合,我从 Postman Runner 运行,但无法解析响应.

I have a collection with this two webservices and i run from Postman Runner, but don't work to parse the response.

谁能帮帮我?谢谢!:)

Can anyone help me? Thx! :)

推荐答案

关键在于如何将 XML 转换为 JSON 以及如何访问 JSON 数据.

The trick is how your XML is converted to JSON and how to access JSON data.

您解析的 JSON 具有下一个结构:因此,为了从中获取值,您应该像这样一步一步地获取元素:

Your parsed JSON has the next structure: So in order to get a value from it you should get elements step by step like this:

jsonData["xs:schema"]["xs:element"][0]["$"]["name"]

所以要设置变量,您需要这样做:

So to set up the variable you need to do this:

postman.setEnvironmentVariable("NumberReq", jsonData["soap:Body"]["ns2:execResponse"]["ns2:execReturn"]);

希望上面的示例正是您需要的代码.如果不是,请修改parced JSON的结构并更改为您需要的元素的方式.

Hope that example above is exactly the code you need. If not, revise the structure of the parced JSON and change the way to the element you need.

也许您也可以使用点"结构来实现,例如 jsonData.Body.execResponse.execReturn

Probably you also can do it using "dot" structure, like jsonData.Body.execResponse.execReturn

这篇关于在 Postman 中解析对另一个请求的 XML 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆