Postman scripts reference
pm
pm.info.eventName:String
: the type of scripts that is currently running (preprocessor script or postprocessor script).pm.info.iteration:Number
: the number of the current iteration(only valid in test collections).pm.info.iterationCount:Number
: the number of total iterations (only valid in test collections).pm.info.requestName:String
: the name of the current API running.pm.info.requestId:String
: the ID of the current API running.pm.sendRequest
pm.sendRequest:Function
pm.sendRequest
is used to send HTTP/HTTPS requests asynchronously in scripts.
pm.variables
pm.variables:
view Variable SDK documentation here.Local Variables
> Environment Variables
> Global Variables Shared within Project
> Global Variables Shared within Team
.pm.variables.has(variableName:String):function → Boolean
: Check whether a temporary variable exists.pm.variables.get(variableName:String):function → *
: get a temporary variable.pm.variables.set(variableName:String, variableValue:String):function → void
: set a temporary variable.pm.variables.replaceIn(variableName:String):function
: Replaces "dynamic variables" within a string (e.g., {{variable_name}}) with actual values. Example:// Define a string containing a dynamic variable
let stringWithVariable = "Hello, {{username}}";
// Use the replaceIn method to replace the {{username}} placeholder
let realValueString = pm.variables.replaceIn(stringWithVariable);
// Output the replaced value
console.log(realValueString); // Output: "Hello, john_doe"
pm.variables.replaceInAsync(variableName:String):function
: Replaces "dynamic value expressions" within a string (e.g., {{$person.fullName}}) with actual values. This method returns a Promise, so you need to use await when calling it. Example:// Define a string containing a dynamic value expression
let stringWithVariable = "Hello, {{$person.fullName}}";
// Use the replaceInAsync method to replace the {{$person.fullName}}
let realValueString = await pm.variables.replaceInAsync(stringWithVariable);
pm.variables.toObject():function → Object
: get all local variables as objects.pm.iterationData
pm.iterationData:
pm.iterationData.has(variableName:String):function → Boolean
: Check whether a test variable exists.pm.iterationData.get(variableName:String):function → *
: get a test variable.pm.iterationData.replaceIn(variableName:String):function
: replace dynamic variables in a string with their actual values, for example, {{variable_name}}
.pm.iterationData.toObject():function → Object
: get all local variables as objects.pm.environment
pm.environment.name:String
: the environment name.pm.environment.has(variableName:String):function → Boolean
:Check whether an environment variable exists.pm.environment.get(variableName:String):function → *
: get an environment variable.pm.environment.set(variableName:String, variableValue:String):function
:set an environment variable.pm.environment.replaceIn(variableName:String):function
: replace dynamic variables in a string with their actual values, for example, {{variable_name}}
.pm.environment.toObject():function → Object
: get all local variables as objects.pm.environment.unset(variableName:String):function
: unset an environment variable.pm.environment.clear():function
: clear all environment variables under the current environment.pm.globals
pm.globals.has(variableName:String):function → Boolean
: Check whether a global variable exists.pm.globals.get(variableName:String,variableScope:String):function → *
: get a global variable. Use 'PROJECT' (default) or 'TEAM' to specify the variable's scope.pm.globals.set(variableName:String,variableValue:String, variableScope:String):function
: set a global variable. Use 'PROJECT' (default) or 'TEAM' to specify the variable's scope.pm.globals.replaceIn(variableName:String):function
: replace dynamic variables in a string with their actual values, for example, {{variable_name}}
.In order to get the value of a request parameter that contains a variable in preprocessor scripts, use pm.globals.replaceIn
to replace the variable with the real value.
pm.globals.toObject():function → Object
: get all global variables as objects.pm.globals.unset(variableName:String,variableScope:String):function
: unset a global variable. Use 'PROJECT' (default) or 'TEAM' to specify the variable's scope.pm.globals.clear():function
: clear all global variables under the current environment.TIP
1.
current values
, notinitial values
.2.
pm.request
pm.request
: view Request SDK documentation here.request
is the API request object. In the preprocessor script, it is the request that will be sent. In the postprocessor script, it is the request that has already been sent.request
includes the following information:pm.request.url
:Url: the URL of the current request.pm.request.getBaseUrl()
: Retrieves BASE URL
selected by the current runtime environment. This feature is supported after version 2.1.39.pm.request.headers
:HeaderList: the header list of the current request.pm.request.method
:String: the method of the current request, such as GET, POST, etc.pm.request.body
: RequestBody: the body of the current request.pm.request.headers.add({ key: headerName:String, value: headerValue:String}):function
: Add a header with a key, headerName, in the current request.pm.request.headers.remove(headerName:String):function
: Delete a header with a key, headerName, in the current request.pm.request.headers.upsert({ key: headerName:String, value: headerValue:String}):function
: Upsert a header with a key, headerName, in the current request. If the key already exists, it will be modified.The following API can only be used in postprocessor scripts
.
pm.response
pm.response
: view Response SDK documentation here.pm.response
to access return response information in postprocessor scripts.pm.response.code:Number
pm.response.status:String
pm.response.headers
:HeaderListpm.response.responseTime:Number
pm.response.responseSize:Number
pm.response.text():Function → String
pm.response.json():Function → Object
pm.cookies
pm.cookies.has(cookieName:String):Function → Boolean
Check whether the cookie value of a cookieName exists.
pm.cookies.get(cookieName:String):Function → String
Get cookie value from cookieName.
pm.cookies.toObject:Function → Object
Get all cookies under the current domain as an object.
pm.cookies.jar().clear(pm.request.url)
Clear all cookies.
TIP
pm.test
done
(an optional parameter) in a callback function.
pm.test.index():Function → Number
Get the total number tests from a specific location.
pm.expect
pm.expect
is an assertion method. View ChaiJS expects BDD library documentation here.pm.response.to.have.*
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
for quick assertions.pm.response.to.be.info
Check whether the status code is 1XX.
pm.response.to.be.success
Check whether the status code is 2XX.
pm.response.to.be.redirection
Check whether the status code is 3XX.
pm.response.to.be.clientError
Check whether the status code is 4XX.
pm.response.to.be.serverError
Check whether the status code is 5XX.
pm.response.to.be.error
Check whether the status code is 4XX or 5XX.
pm.response.to.be.ok
Check whether the status code is 200.
pm.response.to.be.accepted
Check whether the status code is 202.
pm.response.to.be.badRequest
Check whether the status code is 400.
pm.response.to.be.unauthorized
Check whether the status code is 401.
pm.response.to.be.forbidden
Check whether the status code is 403.
pm.response.to.be.notFound
Check whether the status code is 404.
pm.response.to.be.rateLimited
Check whether the status code is 429.
Modified at 2025-01-15 09:50:42