Postman使用
Postman作为一个API测试和交付工具,在开发中被广泛应用。
变量设置
变量以引用的方式广泛用于URL和请求参数设置,主要有两种添加变量的方式
- 项目Variables- 只对当前项目/集合起作用
- 系统Environment- 对所有项目/集合起作用
两者各有优点,但是为了更好的发送Postman文件,建议使用项目Variables,因为使用项目Variables只需要发送一个API文件即可,而如果使用系统Environment,需要发送两个文件(API+Environment)
注释
为了减少沟通成本和方便使用,最好的方法就是添加注释,尤其是给请求body中的数据添加说明
注释问题
在请求中添加的注释和标注都会被发送到服务器端,引发解析异常。
移除注释(发送时)
为了解决注释发送到服务器端的问题,可以通过设置Pre-request Script的方法,在发送请求之前对数据进行预处理
请注意Pre-request Script是设置在整个请求集合上的,而不是单个请求上的
Script脚本代码如下所示
/**
* https://github.com/fkei/JSON.minify/blob/master/minify.json.js
*/
(function(global){
/**
* @function
* @memberof JSON
* @param {Object} Transformed data. format) json-like
* @return {String}
*
* @example
* var json = { // hoge
* "foo": "bar",// this is cool
* "bar": [
* "baz", "bum", "zam" // this is cool
* ]
* } // hoge
*
*/
var minify = function (json) {
var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r|\[|]/g,
in_string = false,
in_multiline_comment = false,
in_singleline_comment = false,
tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc,
prevFrom
;
tokenizer.lastIndex = 0;
while ( tmp = tokenizer.exec(json) ) {
lc = RegExp.leftContext;
rc = RegExp.rightContext;
if (!in_multiline_comment && !in_singleline_comment) {
tmp2 = lc.substring(from);
if (!in_string) {
tmp2 = tmp2.replace(/(\n|\r|\s)*/g,"");
}
new_str[ns++] = tmp2;
}
prevFrom = from;
from = tokenizer.lastIndex;
// found a " character, and we're not currently in
// a comment? check for previous `\` escaping immediately
// leftward adjacent to this match
if (tmp[0] === "\"" && !in_multiline_comment && !in_singleline_comment) {
// limit left-context matching to only go back
// to the position of the last token match
//
// see: https://github.com/getify/JSON.minify/issues/64
lc.lastIndex = prevFrom;
// perform leftward adjacent escaping match
tmp2 = lc.match(/(\\)*$/);
// start of string with ", or unescaped " character found to end string?
if (!in_string || !tmp2 || (tmp2[0].length % 2) === 0) {
in_string = !in_string;
}
from--; // include " character in next catch
rc = json.substring(from);
}
else if (tmp[0] === "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) {
in_multiline_comment = true;
}
else if (tmp[0] === "*/" && !in_string && in_multiline_comment && !in_singleline_comment) {
in_multiline_comment = false;
}
else if (tmp[0] === "//" && !in_string && !in_multiline_comment && !in_singleline_comment) {
in_singleline_comment = true;
}
else if ((tmp[0] === "\n" || tmp[0] === "\r") && !in_string && !in_multiline_comment && in_singleline_comment) {
in_singleline_comment = false;
}
else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) {
new_str[ns++] = tmp[0];
}
}
new_str[ns++] = rc;
return new_str.join("");
};
if (typeof module !== 'undefined' && module.exports) {
// node
module.exports = minify;
JSON.minify = minify;
} else {
// others, export global
if (typeof global.JSON === "undefined" || !global.JSON) {
global.JSON = {};
}
global.JSON.minify = minify;
}
})(this);
//对发送数据进行处理
pm.request.body.raw = JSON.minify(pm.request.body.raw)
通过以上script脚本就解决了客户端注释发送问题。
添加Example
在Postman中,可以为请求添加example,添加方式下图所示
在example中可以任意添加注释信息,而且也可以添加多个example
在example可以添加各种注释和标注信息,可以当作文档使用。
设置返回数据为变量
一般通过Environment或Variables统一设置请求变量,特别是需要授权的API请求,一般会设置一个token变量,然后附加到请求header中。
一般情况下,会先调用授权API获取请求token,然后把返回的token复制到变量中使用,操作比较繁琐。
Postman提供了在Tests中添加脚本的功能,脚本把请求返回的数据直接设置到变量中,这样就不用每次copy了。
在脚本中定义变量方法如下:
Method | Use-case | Example |
---|---|---|
pm.globals |
Use to define a global variable. | pm.globals.set("variable_key", "variable_value"); |
pm.collectionVariables |
Use to define a collection variable. | pm.collectionVariables.set("variable_key", "variable_value"); |
pm.environment |
Use to define an environment variable in the currently selected environment. | pm.environment.set("variable_key", "variable_value"); |
pm.variables |
Use to define a local variable. | pm.variables.set("variable_key", "variable_value"); |
unset |
You can use unset to remove a variable. |
pm.environment.unset("variable_key"); |
设置方法如下:
Script脚本代码如下所示
var jsonData = JSON.parse(responseBody);
pm.collectionVariables.set("flaskz_auth_token",jsonData['data']['token']);
通过以上设置,当调用授权API后,会自动把返回的token设置到变量中供其他API使用。
通过Script移除请求中的注释信息和为请求添加example,就可以在Postman中添加各种注释信息,从而减少沟通和使用成本。
相比于专业的API文档工具,Postman的这种文档稍显简单,但是对于开发人员来说,很可能没时间写大量的API文档,而Postman这种方式既能节约时间,又简洁高效。