pm 脚本 API
pm
pm:Object
pm
对象包含了接口(或测试集)运行的相关信息,并且可以通过它访问需要发送的请求信息和发送后返回的响应信息。另外还可以通过它获取 (get) 或设置 (set) 环境变量和全局变量。pm.info:Object
pm.info
对象包含了接口(或测试集)运行的相关信息。pm.info.eventName:String
pm.info.iteration:Number
pm.info.iterationCount:Number
pm.info.requestName:String
pm.info.requestId:String
pm.sendRequest
pm.sendRequest:Function
pm.sendRequest
用途为在脚本内异步发送 HTTP/HTTPS 请求。
pm.variables
pm.variables:
Variable SDK 参考临时变量
> 环境变量
> 项目全局变量
> 团队全局变量
。pm.variables.has(variableName:String):function → Boolean
: 检查是否存在某个临时变量。pm.variables.get(variableName:String):function → *
: get 单个临时变量。pm.variables.set(variableName:String, variableValue:String):function → void
: set 单个临时变量。pm.variables.replaceIn(variableName:String):function
: 以真实的值替换字符串里包含的“动态变量”,如{{variable_name}}
。示例:
pm.variables.replaceInAsync(variableName:String):function
: 以真实的值替换字符串里包含的“动态值表达式”,如{{$person.fullName}}
。返回 Promise,调用时需加 await
。示例:
pm.variables.toObject():function → Object
: 以对象形式获取所有临时变量。pm.iterationData
pm.iterationData:
pm.iterationData.has(variableName:String):function → Boolean
: 检查是否存在某个测试数据变量。pm.iterationData.get(variableName:String):function → *
: get 单个测试数据变量。pm.iterationData.replaceIn(variableName:String):function
: 以真实的值替换字符串里包含的“动态变量”,如{{variable_name}}
。pm.iterationData.toObject():function → Object
: 以对象形式获取所有测试数据变量。pm.environment
pm.environment.name:String
: 环境名。pm.environment.has(variableName:String):function → Boolean
:检查是否存在某个环境变量。pm.environment.get(variableName:String):function → *
:get 单个环境变量。pm.environment.set(variableName:String, variableValue:String):function
:set 单个环境变量。pm.environment.replaceIn(variableName:String):function
:以真实的值替换字符串里的包含的动态变量,如{{variable_name}}
。pm.environment.toObject():function → Object
:以对象形式获取当前环境的所有变量。pm.environment.unset(variableName:String):function
: unset 单个环境变量。pm.environment.clear():function
:清空当前环境的所有变量。本地值
,而不会读写远程值
。pm.globals
pm.globals.has(variableName:String):function → Boolean
:检查是否存在某个全局变量。pm.globals.get(variableName:String,variableScope:String):function → *
:get 单个全局变量。 可以使用 'PROJECT' (默认)或 'TEAM' 来选择全局变量范围。pm.globals.set(variableName:String,variableValue:String, variableScope:String):function
:set 单个全局变量。可以使用 'PROJECT' (默认)或 'TEAM' 来选择全局变量范围。pm.globals.replaceIn(variableName:String):function
:以真实的值替换字符串里的包含的动态变量,如{{variable_name}}
。如前置脚本,获取请求参数的值如果包含变量,则需要使用 pm.globals.replaceIn
才能将变量替换会真正的值。
pm.globals.toObject():function → Object
:以对象形式获取所有全局变量。pm.globals.unset(variableName:String):function
: unset 单个全局变量。pm.globals.unset(variableName:String,variableScope:String):function
: unset 单个全局变量。可以使用 'PROJECT' (默认)或 'TEAM' 来选择全局变量范围。pm.globals.clear():function
:清空当前环境的全局变量。1.
本地值
,而不会读写远程值
。2.
本地值
。如果当前不存在此名称的团队变量,则不会新增一个团队变量,而是把本次 set 的变量当做临时变量来使用。pm.request
pm.request:
Request SDK 参考request
是接口请求对象。在前置脚本中表示将要发送的请求
,在后置脚本中表示已经发送了的请求
。request
包含了以下结构:pm.request.url
:Url
: 当前请求的 URL。pm.request.getBaseUrl()
:获取当前运行环境选择的的 前置 URL
,在 2.1.39 版本之后支持。pm.request.headers
:HeaderList
:当前请求的 headers 列表。pm.request.method
:String
当前请求的方法,如GET
、POST
等。pm.request.body
:RequestBody
: 当前请求的 body 体。pm.request.headers.add({ key: headerName:String, value: headerValue:String})
:function
: 给当前请求添加一个 key 为headerName
的 header。pm.request.headers.remove(headerName:String)
:function
: 删除当前请求里 key 为headerName
的 headerpm.request.headers.get(headerName:String)
:function
: 查询当前请求里的 headerName
。pm.request.headers.upsert({ key: headerName:String, value: headerValue:String})
:function
: upsert key 为headerName
的 header(如不存在则新增,如已存在则修改)。pm.request.auth
: 当前请求的身份验证信息Accept
值并打印到控制台。pm.response
以下部分 API 仅在 后置脚本
中可用
pm.response:
Response SDK 参考pm.response
接口请求完成后返回的 response 信息。pm.response.code:Number
pm.response.status:String
pm.response.headers:
HeaderList
pm.response.responseTime:Number
pm.response.responseSize:Number
pm.response.text():Function → String
pm.response.json():Function → Object
pm.response.setBody('')
pm.response.headers.get
: Response SDK 参考pm.response.headers.get
命令可以提取返回响应中的 headers
中的值。例如想要在控制台中显示 Header 中的 Date 值,那么可以在后置操作中填写如下自定义脚本:

pm.cookies
pm.cookies:
CookieList SDK 参考cookies
为当前请求对应域名下的 cookie 列表。pm.cookies.has(cookieName:String):Function → Boolean
cookieName
的 cookie 值pm.cookies.get(cookieName:String):Function → String
cookieName
的 cookie 值pm.cookies.toObject:Function → Object
pm.cookies.jar().clear(pm.request.getBaseUrl())
pm.test
pm.test(testName:String, specFunction:Function):Function
done
,还可用来测试异步方法:
pm.test.index():Function → Number
pm.expect
pm.expect(assertion:*):Function → Assertion
pm.expect
是一个普通的断言方法,查看详细的说明:ChaiJS expect BDD library。response
或 variables
里的数据非常有用,更多关于 pm.expect
断言的是示例,可以点击这里查看:Assertion library examplesResponse 对象可用的断言 API 列表
pm.response.to.have.status(code:Number)
pm.response.to.have.status(reason:String)
pm.response.to.have.header(key:String)
pm.response.to.have.header(key:String, optionalValue:String)
pm.response.to.have.body()
pm.response.to.have.body(optionalValue:String)
pm.response.to.have.body(optionalValue:RegExp)
pm.response.to.have.jsonBody()
pm.response.to.have.jsonBody(optionalExpectEqual:Object)
pm.response.to.have.jsonBody(optionalExpectPath:String)
pm.response.to.have.jsonBody(optionalExpectPath:String, optionalValue:*)
pm.response.to.have.jsonSchema(schema:Object)
pm.response.to.have.jsonSchema(schema:Object, ajvOptions:Object)
pm.response.to.be
pm.response.to.be
是用来快速断言的一系列内置规则。pm.response.to.be.info
1XX
pm.response.to.be.success
2XX
pm.response.to.be.redirection
3XX
pm.response.to.be.clientError
4XX
pm.response.to.be.serverError
5XX
pm.response.to.be.error
4XX
或5XX
pm.response.to.be.ok
200
pm.response.to.be.accepted
202
pm.response.to.be.badRequest
400
pm.response.to.be.unauthorized
401
pm.response.to.be.forbidden
403
pm.response.to.be.notFound
404
pm.response.to.be.rateLimited
429
修改于 2025-01-16 06:48:08