邮递员:有没有办法在执行过程中弄清楚数据文件的类型/名称?

时间:2017-08-07 09:13:38

标签: postman

我需要创建预先请求脚本,根据数据是通过json还是csv文件来调整行为。

是否有任何运行时机制来确定数据文件类型或数据文件的名称?

帮助我做以下事情的事情,

if (data.fileType === "json") {
 //data interpretation as per json hierarchy
} else {
 //data interpretation as per csv structure
}

在这里,我可以用什么来代替data.fileType

或者是否可以找出data.fileName

1 个答案:

答案 0 :(得分:1)

您可以尝试将data解析为json。

var parsedData;

try {
   parsedData = JSON.parse(data); // Will throw an error if data not JSON
   // Your logic to parse data as JSON goes here
} catch (e) {
   parsedData = data; // data is not JSON, check for CSV
   // Your logic to parse as CSV goes here
}