Skip to content

Latest commit

 

History

History
67 lines (46 loc) · 2.37 KB

File metadata and controls

67 lines (46 loc) · 2.37 KB

What is Postman

As it's website introduces Postman is the most complete toolchain for API development

Environments

Create environment

Click the gear icon and choose Manage environments.

Variables

Link: https://www.getpostman.com/docs/postman/environments_and_globals/variables

Create variables for environments, e.g.:

key value
google_url http://google.com
key Postman
content_json application/json

You can use it in your requests, for e.g:

  • Request URL: {{google_url}}/?q={{key}}, means http://google.com/?q=Postman
  • Headers: Content-type:{{content_json}}

Postman has those auto generated variables:

  • {{$guid}} : Adds a v4 style guid
  • {{$timestamp}}: Adds the current timestamp
  • {{$randomInt}}: Adds a random integer between 0 and 1000

Write test scripts

Link: https://www.getpostman.com/docs/postman/scripts/test_scripts

You can write tests for result by javascript:

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

var jsonData = JSON.parse(responseBody);
tests["Has response_code"] = jsonData.hasOwnProperty('response_code');
tests["response_code is numeric"] = isNumber(jsonData.response_code);
tests["Has message"] = jsonData.hasOwnProperty('message');

if (jsonData.hasOwnProperty('data')) {
    tests["[Data] has age"] = jsonData.data.hasOwnProperty('age');
    tests["[Data] has name"] = jsonData.data.hasOwnProperty('name');
    tests["[Data] has address"] = jsonData.data.hasOwnProperty('address');
    console.log("name: " + jsonData.data.name);
}
console.log(jsonData);

Enable console

For console.log or any similar command, you have to enable Developer Tools for Postman:

  1. Browse chrome://flags on Google Chrome
  2. Enable Debugging for packed apps
  3. Right-click on Postman screen, choose Inspect or Ctrl + Shift + I on Windows for short.

Generate code snippets

Link: https://www.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets