json 如何使用 Postman 从 XML 中提取变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36815414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:24:11  来源:igfitidea点击:

How do I extract a variable from XML using Postman?

jsonxmlpostman

提问by Kyle Burriss

I'm trying to extract a SessionId from the XML which is returned from a SOAP API.

我正在尝试从 SOAP API 返回的 XML 中提取 SessionId。

I've read through the Postman documentation (several times over) but it wasn't the most helpful in achieving my goal.

我已经通读了 Postman 文档(多次),但这对实现我的目标并不是最有帮助的。

What was suggested in a few blogs was to convert the XML to JSON, and then pick out the token and it's value from there, but that didn't help either.

一些博客中建议将 XML 转换为 JSON,然后从那里挑选令牌及其值,但这也无济于事。

I used the following in my Test:

我在测试中使用了以下内容:

var jsonObject = xml2Json(responseBody);
postman.setGlobalVariable("Session_Id", jsonObject.SessionID);

The above created the variable "Session_Id" but didn't actually assign a value to it. I'm stumped.

上面创建了变量“Session_Id”,但实际上并没有为其赋值。我难住了。

I'm definitely retrieving the data from the API, and it's viewable in Postman's "Body" Response.

我肯定是从 API 检索数据,并且可以在 Postman 的“Body”响应中查看。

回答by Kyle Burriss

To extract a variable from XML using Postman, first convert your XML to JSON, using the xml2Json converter method:

要使用 Postman 从 XML 中提取变量,首先使用 xml2Json 转换器方法将您的 XML 转换为 JSON:

var responseJson = xml2Json(responseBody);

(Where "responseBody" is your xml body.) Then use the console.log method to output your JSON data, as such:

(其中“responseBody”是您的 xml 正文。)然后使用 console.log 方法输出您的 JSON 数据,如下所示:

console.log(responseJson);

Be sure to have followed this tutorial on Enabling Chrome Dev Tools in Postman

请务必遵循有关在 Postman 中启用 Chrome 开发工具的教程

Inside your Test Runner, run the test, then right click and "Inspect" anywhere in the Runner. Select the "Console" tab once Chrome's Dev Tools launch. Expand the "Object" part.

在您的测试运行器中,运行测试,然后右键单击并在运行器中的任意位置“检查”。Chrome 的开发工具启动后,选择“控制台”选项卡。展开“对象”部分。

Then drill-down (expand) until you see the Property whose data you need. Thereafter, set the variable by appending each drill-down level to the parameter you want:

然后向下钻取(展开),直到看到您需要其数据的属性。此后,通过将每个向下钻取级别附加到您想要的参数来设置变量:

postman.setGlobalVariable("Session_Id", responseJson.UserSessionToken.SessionID); 

In this case, "responseJson" is the object, "UserSessionToken" was the next level in the drill-down, and SessionId was the parameter I needed within that.

在这种情况下,“responseJson”是对象,“UserSessionToken”是深入研究的下一个级别,SessionId 是我需要的参数。

回答by tom redfern

Since Postman v7.15.0 the accepted answer did not work for me (it used to before the update). You have to put the path segments into square brackets.

从 Postman v7.15.0 开始,接受的答案对我不起作用(它在更新之前曾经使用过)。您必须将路径段放入方括号中。

For example, in the following XML response:

例如,在以下 XML 响应中:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<QuotesPrivateResponse>
    <lease-service>
        <duration-in-months>24</duration-in-months>
    </lease-service>
</QuotesPrivateResponse>

to retrieve the value duration-in-months:

检索值duration-in-month

var response = xml2Json(responseBody);
var duration = response["QuotesPrivateResponse"]["lease-service"]["duration-in-months"];
pm.environment.set("duration", duration);

回答by tuxErrante

Postman v7.20.1

邮递员 v7.20.1

I'd like to add my answer since above there are a couple of details that took me a while to solve:

我想添加我的答案,因为上面有几个细节让我花了一段时间才解决:

  • how to cope with a multipart SOAP response
  • how to manage a JSON object
  • responseBody definition
  • 如何处理多部分 SOAP 响应
  • 如何管理 JSON 对象
  • 响应体定义

Here's the first lines of my XML response to analyze:

这是我要分析的 XML 响应的第一行:

------=_Part_694527_1470506002.1584708814081
Content-Type: application/xop+xml;charset=UTF-8;type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: 
<e3bd82ac-d88f-49d4-8088-e07ff1c8d407>
    <?xml version="1.0" encoding="UTF-8" ?>
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
        <env:Header/>
        <env:Body>
            <ns2:GenericResponse xmlns:ns2="http://XXXXXXXX">
                <ns2:Service IdcService="XXXXXXXX">
                    <ns2:Document>
                        <ns2:Field name="XXXXXXXX:isSetDefault">1</ns2:Field>
                        <ns2:Field name="XXXXXXXX">CHECKIN_UNIVERSAL</ns2:Field>

After I noticed it was a multipart I've ended up with this Postman Test:

在我注意到它是一个多部分之后,我最终得到了这个 Postman 测试:

var response = pm.response.text();
var responseBody = response.substr(response.indexOf('<env:')); 

pm.test("The body of the response is a valid XML", function () {
     pm.expect(xml2Json(responseBody)).to.exist;
});


pm.test("UCM file upload checkin succesfull", function(){

    var responseJson = xml2Json(responseBody);
    var JsonFields = (responseJson['env:Envelope']['env:Body']['ns2:GenericResponse']['ns2:Service']['ns2:Document']['ns2:Field']);

    JsonFields.forEach( field => {
    if (field.name == 'StatusMessage'){
        console.log("Field = " + field.name);
        pm.expect(field.text().to.include("Successfully checked in"));
        }
    }); 
});