如何使用Postman从XML提取变量? [英] How do I extract a variable from XML using Postman?

查看:1278
本文介绍了如何使用Postman从XML提取变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经阅读了几次Postman文档,但这对实现我的目标并不是最有帮助.

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

在一些博客中建议将XML转换为JSON,然后从中选择令牌及其价值,但这也无济于事.

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.

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

I used the following in my Test:

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

上面创建了变量"Session_Id",但实际上并未为其分配值.我很困惑

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

我肯定是从API检索数据,并且可以在Postman的正文"响应中查看.

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

推荐答案

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

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

var responseJson = xml2Json(responseBody);

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

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

console.log(responseJson);

请务必遵循在Postman中启用Chrome开发工具

在测试运行器"内部,运行测试,然后右键单击并检查"运行器中的任何位置. 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.

然后向下钻取(展开),直到看到该属性所需的数据. 之后,通过将每个向下钻取级别附加到所需的参数来设置变量:

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); 

在这种情况下,"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.

这篇关于如何使用Postman从XML提取变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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