Making REST API Calls with Postman



Why Postman and How to Get it?



• Quickly test APIs in GUI
• Save APIs into Collection for reuse
• Manage multiple environment
• Auto generate code from API calls
• Standalone application or Chrome plugin
https://www.getpostman.com

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sending an API Request



• Choose method
• Enter URI
• Configure headers and authentication
• Provide data
• Send and verify status
• Enter postman-echo.com/get into the URL field and click Send

images/107-1.png

• You see Status: 200 OK in the response row in Postman and you should get this JSON back as a response body, with your system's value for postman-token

{
    "args": {
        "aaaUser": {
            "attributes": {
                "name""{{username}}",
                "pwd""{{password}}"
            }
        }
    },
    "headers": {
        "x-forwarded-proto""https",
        "host""postman-echo.com",
        "content-length""130",
        "accept""*/*",
        "accept-encoding""gzip, deflate",
        "cache-control""no-cache",
        "content-type""application/json",
        "postman-token""d741c86b-7c4d-4923-b1d7-11af72cc3ccb",
        "user-agent""PostmanRuntime/7.11.0",
        "x-forwarded-port""80"
    },
    "url""https://postman-echo.com/get"
}


images/107-2.png

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Working with API Collections



• With an API collection you store and group API requests into folders
• Use a Postman collection when you want to use the same variable across many requests

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Using Environments and Variables



• Use a Postman environment when you want to protect credentials or an API endpoint

Variables Make Requests Reusable and Flexible



• Never good to hardcode details
• What if you want to connect to different host?
• What if credentials change?
• Variable References

{{ apic }}          # host
{{ username }}
{{ password }}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Managing Environments



• To create an environment, click the Settings (gear) icon in the right-hand side of Postman and choose Manage Environments.
• Create any number of environment needed
• Change between environments with drop down list
• Add as many variables as needed
• Reference anywhere with {{ variable name }} syntax

Setting Environment Variables Dynamically



• What about when info from one request is needed from another?
• Manually copying / pasting slow and error prone
• Manually updating environment variables is slow and awkward

images/107-3.png

• To use a variable, add double curly brackets {{variable}} around the value you want to change within the Postman interface
• You can store endpoints you use frequently and then change between environments with a drop-down list

Tests Enable Dynamic Environment Variables



• Each API request offers both pre and post actions
◇ Pre → Pre-request script
◇ Post → Tests
• Written in JavaScript

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Using Postman to Generate Code!



You'll eventually want to write some code...



• ** Note ** Maximize your Postman Window to use this feature. Otherwise, you can't see command options available
• Postman great for testing and validating APIs
• But it's about atomic actons
• Business Logic, stringing APIs together, etc all need code
• Jumpstart with auto-generated code by Postman
• Many, many options for languages available

Full API Request to Code!



• Headers, payload data and URI all included
• Environment variables are translated
• Great starting point, but expect to edit and update


Index