Find a pm API member#
Every pm member, with what it returns and which phase it belongs to.
This page lists the pm API available to pre-request scripts and tests. The editor suggests these members as you type, so you rarely need to memorise them. The full list is here for reference. Members that read the response are useful only in a test script. There is no response before the request goes out.
Variable scopes#
pm.environment, pm.globals, and pm.collectionVariables each read and write one scope. GraphDagger saves the writes after the script ends.
| Call | What it does |
|---|---|
get(key) | Read a variable. Returns undefined when unset. |
set(key, value) | Create or update a variable. |
unset(key) | Remove a variable. |
has(key) | Return true when the scope holds the key. |
toObject() | Return every variable in the scope as an object. |
pm.variables reads across all scopes but writes only to the current run:
| Call | What it does |
|---|---|
get(key) | Read a variable from any scope. |
set(key, value) | Write a variable for this run only, not saved. |
has(key) | Return true when any scope holds the key. |
replaceIn(template) | Resolve {{variable}} and {{$dynamic}} tokens in a string. |
The pm.request members#
pm.request reads the outgoing request. In a pre-request script it also edits the request.
| Member | What it does |
|---|---|
pm.request.method | The HTTP method. Assign to change it. |
pm.request.url | The URL. Assign to change it. |
pm.request.headers.get(name) | Read a header, case-insensitive. |
pm.request.headers.has(name) | Return true when the header is present. |
pm.request.headers.upsert({key, value}) | Add or replace a header. |
pm.request.headers.remove(name) | Remove a header. |
pm.request.body.raw | The raw request body string. Assign to change it. |
The pm.response members#
pm.response reads the response. It is available in a test script.
| Member | What it does |
|---|---|
pm.response.code | The status code as a number. |
pm.response.status | The status text. |
pm.response.responseTime | The round-trip time in milliseconds. |
pm.response.json() | Parse the body as JSON. Throws on invalid JSON. |
pm.response.text() | Return the body as text. |
pm.response.headers.get(name) | Read a response header, case-insensitive. |
pm.response.to.have.status(code) | Assert the status equals code. |
pm.response.to.have.header(name) | Assert the header is present. |
pm.response.to.be.ok | Assert the status is in the 2xx range. |
Tests and assertions#
| Call | What it does |
|---|---|
pm.test(name, fn) | Register a named test; fn runs and records pass or fail. |
pm.expect(value) | Start an assertion chain. |
An assertion chain reads left to right. It uses linking words (to, be, have, that, which, deep) and ends in a check: equal, eql (deep equality), include, a or an, above, below, property, status, or header. It also offers the getters true, false, null, undefined, and ok. Put not before a check to negate it. The assertion set is compact, so words from other assertion libraries such as length, match, or throw are not available.
Info, sending, and logging#
| Call | What it does |
|---|---|
pm.info.eventName | Which phase is running, prerequest or test. |
pm.info.requestName | The name of the current request. |
pm.sendRequest(reqOrUrl, cb) | Send another request from the script. |
console.log / warn / error / info / debug | Write a line to the Console tab. |