Sei sulla pagina 1di 1

Postman CHEAT SHEET

About DYNAMIC VARIABLES


Experimental. Can only be used in request builder. Only
Assertions Postman Sandbox
Thank you for downloading this cheat sheet. This guide one value is generated per request. pm
Note: You need to add any of the assertions inside a
refers to the Postman App, not the Chrome extension. pm.test callback. Example This is the object containing the script that is running, can
Please report any problems with it. {{$guid}} - global unique identifier. Example pm.test("Status code is 200", function () { set variables and has access to a read-only copy of the
output: d96d398a-b655-4638-a6e5-40c0dc282fb7 pm.response.to.have.status(200); request or response.
Postman Cheat Sheet is based on the official Postman });
documentation and own experience. pm.globals
{{$timestamp}} - current timestamp. Example Status code Handle global variables.
For a detailed documentation on each feature, check out output: 1507370977
Check if status code is 200: Available methods: has, get, set, unset, clear, toObject.
https://www.getpostman.com/docs. pm.response.to.have.status(200);
{{$randomInt}} - random integer between 0 and pm.globals.get("myVariable");

Variables 1000. Example output: 567


All dynamic variables can be combined with strings, in
Checking multiple status codes:
pm.expect(pm.response.code).to.be.oneOf([201,202]); pm.environment
SETTING order to generate dynamic / unique data. Example body: Response time Handle environment variables.
Available methods: has, get, set, unset, clear, toObject.
Response time below 9ms:
Global {"name": "John Doe", "email":
pm.expect(pm.response.responseTime).to.be.below(9);
"john.doe.{{$timestamp}}@example.com"} pm.environment.set("myVar", "MY_VALUE");
pm.globals.set("myVariable", MY_VALUE); Headers
LOGGING / DEBUGGING VARIABLES pm.sendRequest
Environment Header exists:
Open Postman Console and use console.log in your script. Send simple HTTP(S) GET requests from scripts:
pm.response.to.have.header(”X-Cache");
pm.environment.set("myVar", MY_VAL); Example: pm.sendRequest('http://example.com',
Header has value: function (err, res) {
Data variables var myVar = pm.globals.get("myVar"); pm.expect(pm.response.headers.get(X-Cache ')) console.log(err ? err : res.json());
console.log(myVar); .to.eql(HIT'); });
Can only be set from a CSV or a JSON file.
Cookies
Full-option HTTP(S) request
GETTING
in scripts (pre-request, tests)
Workflows Cookie exists:
pm.expect(pm.cookies.has('sessionId')).to.be.true;
const postRequest = {
url: 'http://example.com',
method: 'POST',
for Collection Runner / Newman Cookie has value: header: 'X-Foo:foo',
Global pm.expect(pm.cookies.get('sessionId')).to.eql(’ad3s3'); body: {
mode: 'raw',
pm.globals.get("myVariable"); Set which will be the next request to be executed: raw: JSON.stringify({ name: 'John' })
Body }
Environment postman.setNextRequest(“Request 2); };
Any / HTML responses pm.sendRequest(postRequest, function (err, res) {
pm.environment.get("myVariable");
Stop executing requests / stop the collection run: console.log(err ? err : res.json());
Data variables Exact body match: });
postman.setNextRequest(null); pm.response.to.have.body("OK");
pm.iterationData.get("myVariable"); pm.response.to.have.body("{"success"=true}");

Any variable type Partial body match / body contains: Postman Echo
pm.variables.get("myVariable");
External libraries pm.expect(pm.response.text())
.to.include("Order placed."); Helper API for testing requests. Read more at:
https://docs.postman-echo.com.
in the request builder tv4 - JSON schema validator JSON responses
Syntax: {{myVariable}} var jsonSchema = { "items": { "type": "boolean" }}; Get Current UTC time in pre-request script:
var jsonData1 = [true, false]; Parse body (need for all assertions):
Request URL: http://{{domain}}/users/{{userId}} var jsonData2 = [true, "John"]; var jsonData = pm.response.json(); pm.sendRequest('https://postman-
echo.com/time/now', function (err, res) {
Headers (key, value): X-{{myHeaderName}} pm.test('Schema is valid', function() { Simple value check: if (err) { console.log(err); }
pm.expect(tv4.validate(data1, schema)) pm.expect(jsonData.age).to.eql(30); else {
Body: { "id": "{{userId}}”, "name": "John Doe”} .to.be.true; pm.expect(jsonData.name).to.eql("John"); var currentTime = res.stream.toString();
pm.expect(tv4.validate(data2, schema)) console.log(currentTime);
REMOVING .to.be.true; pm.environment.set("currentTime", currentTime);
Nested value check: }});
console.log("Validation failed: ", tv4.error); pm.expect(jsonData. products.0.category)
Global }); .to.eql(”Detergent");
pm.globals.unset("myVariable"); XML responses
pm.globals.clear(); CryptoJS - cryptographic algorithms Convert XML body to JSON:
Generate SHA-1 hash: var jsonData = xml2Json(responseBody);
Environment
CryptoJS.SHA1("Message").toString(); Note: see assertions for JSON responses.
pm.environment.unset("myVariable");

pm.environment.clear();

The Postman logo and name are property of Postdot Technologies, Inc. • CC BY Valentin Despa • valentin@vdespa.com • www.vdespa.com • Version 0.2.0 • Updated: 2018-02

Potrebbero piacerti anche