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

查看:46
本文介绍了如何使用 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 的Body"响应中查看.

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 开发工具

在您的 Test Runner 中,运行测试,然后右键单击并检查".Runner中的任何地方.选择控制台"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.

注意:这个答案是邮递员v7.15.0之前的正确解决方案.对于高于此的版本,接受的答案可能不起作用.

Note: This answer was the correct solution before v7.15.0 of postman. For versions later than this, the accepted answer may not work.

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

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