从XML响应中提取一个值,并将其设置为Postman中的全局变量 [英] Extract a value from XML response and set it as a global variable in Postman

查看:555
本文介绍了从XML响应中提取一个值,并将其设置为Postman中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Postman从成功的API请求中提取一个值,该请求正在发送XML响应。以下是我尝试从响应中捕获所需值的方法。

I am trying to extract a value from a successful API request, which is sending a XML response, using Postman. Following is the way how I tried to catch the value that I need from the response.

var jsonObject = xml2Json(responseBody);
console.log(jsonObject);

postman.setGlobalVariable("Active_SAML", jsonObject.bankidCollectResponse.SAMLReferens); 
console.log(pm.globals.get("Active_SAML"));

在测试选项卡和控制台注销中编写的脚本如下。

That script written inside "Tests" tab and the console log out put is as below.

但是当我执行程序时,出现以下错误

But when I execute the program, I get following error.


评估测试脚本时出错:TypeError:
无法读取未定义的属性'SAMLReferens'

There was an error in evaluating the test script: TypeError: Cannot read property 'SAMLReferens' of undefined

我不确定我在哪里做错了。有人可以指出吗?

I am not sure where I'm doing it wrong. Can anybody please point out me that?

推荐答案

感谢@ChathurangaChandrasekara的评论,我确实能够弄清他们期望的格式。

Thanks to @ChathurangaChandrasekara comment, I did able to figure out the format that they expected.

// Convert XML output to JSON format
var jsonObject = xml2Json(responseBody);

// Since the converted JSON format is having Envelope and Body tags we need following format
var activeSamlVal = jsonObject['SOAP-ENV:Envelope']['SOAP-ENV:Body'].bankidCollectResponse.SAMLReferens;
console.log(activeSamlVal)

// Assigning the extracted value in to a global variable
pm.globals.set("SAML_key", activeSamlVal);
console.log(pm.globals.get("SAML_key"));

这篇关于从XML响应中提取一个值,并将其设置为Postman中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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