Postman

De Banane Atomic
Aller à la navigationAller à la recherche

Preset Headers

Permet de définir des headers par défaut.

  • YourQuery → Headers tab → presets (on the right)
    • Create a new preset: Manage presets
    • Apply a preset: {PresetName}

Set headers for a collection

In the collection → Pre-request Script

Js.svg
pm.request.headers.add({ key: 'header_name', value: 'header_value' })

Workspace

Use 1 workspace by application.

Environment

Allow to set variables by environnement: local dev prod.
Variable: {{MyVar}}

Import

Swagger

Restricted access Swagger UI

  1. From Swagger UI web page - below the title there is a link to the JSON version
  2. Open it, then save it
  3. From Postman - Import - select the downloaded JSON file

Query

Permet d'importer une requête copiée le navigateur

  • firefox → developer tools → network
  • make a query
  • clique-droit sur la requete → Copy → Copy as cURL
  • Postman → Import → Paste Raw Text

Proxy

Permet de capturer le traffic.

  • Icone antenne (footer en bas à droite)

Tests

Dans une requête → onglet Tests

Js.svg
pm.test("Successful GET request", function () {
    pm.response.to.have.status("OK");
});

pm.test("Returns 5 items", function () {
    const items = pm.response.json();
    pm.expect(items.length).to.eql(5);
});

pm.test("All items should have a name", function () {
    const items = pm.response.json();
    pm.expect(items.every((item) => {
        return item.name !== undefined;
    })).to.be.true;
});

// stocker le résultat dans une variable globale
pm.globals.set("MyVar", pm.response.json().myVar);
// désallouer une varible ue fois que l'on ne s'en sert plus
pm.globals.unset("MyVar");

// appeler la requête suivante
postman.setNextRequest("Request Name");

External libraries

Js.svg
// include external library
const moment = require('moment');

NodeJS modules available:

  • lodash
  • moment
  • path
  • assert
  • buffer
  • util
  • url
  • punycode
  • querystring
  • string_decoder
  • stream
  • timers
  • events

Pre-request script

Ordre d'exécution:

  1. Pre-request script
  2. Request
  3. Response
  4. Test
Js.svg
pm.globals.set("ids", []);

const getItems = {
    url: "http://{{host}}/items",
    method: "GET"
}

pm.sendRequest(getItems, function(err, items)) {
    const ids = _maps(items.json(), function(item) {
        return item.id;
    });

    pm.globals.set("ids", ids);
}

Automatically add Bearer tokens

Js.svg
const tokenUrl = pm.environment.get('oauth-token-url');
const clientId = pm.environment.get('oauth-token-id');
const clientSecret = pm.environment.get('oauth-token-secret');
const scope = 'resource.READ';
const grantType = 'client_credentials';

const getTokenRequest = {
    method: 'POST',
    url: tokenUrl,
    header: [{ key: 'Content-Type', value: 'application/x-www-form-urlencoded' }],
    body: {
        mode: 'urlencoded',
        urlencoded: [
            { key: 'client_id', value: clientId },
            { key: 'client_secret', value: clientSecret },
            { key: 'grant_type', value: grantType },
            { key: 'scope', value: scope }
        ]
    }
};

pm.sendRequest(getTokenRequest, (err, response) => {
    const jsonResponse = response.json();
    newAccessToken = jsonResponse.access_token;

    pm.environment.set('accessToken', newAccessToken);
});

Authorization → Type = BearerToken → Token = {{accessToken}}

Auto-refresh token

If the Auto-refresh token button is greyed out, it is because you didn't fetch a refresh token.

  • on Azure, you have to add offline_access in the Scope

Collections

Permet de sauvegarder des requêtes.

  • Sauvegarder une rêquete: bouton Save à droite
  • Afficher les collections: onglet Collection à gauche

Data

  • Onglet Collections → triangle à droite du nom d'une collection → Run
  • Collection Runner → Data: Select File
Js.svg
// get the items from the data file
const items = data.items;

// get randomly one of the item
var item = _.sample(items);

Visualizer

Js.svg
// in the Tests tab
var template = `
    <table class="styled-table">
        <thead>
            <tr>
                <th>Column 1</th>
            </tr>
        </thead>

        <tbody>
        {{#each response.items}}
            <tr>
                <td>{{value}}</td>
            </tr>
        {{/each}}
        </tbody>
    </table>

    <style>
        body { background-color: rgb(40, 40, 40); }
    </style>
`;

pm.visualizer.set(template, {
    response: pm.response.json()
});
Css.svg
.styled-table {
    border-collapse: collapse;
    margin: 25px 0;
    font-size: 0.9em;
    font-family: sans-serif;
    min-width: 400px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table thead tr {
    background-color: #009879;
    color: #ffffff;
    text-align: left;
}
.styled-table th,
.styled-table td {
    padding: 12px 15px;
}
.styled-table tbody tr {
    border-bottom: 1px solid #dddddd;
    background-color: white;
}
.styled-table tbody tr:last-of-type {
    border-bottom: 2px solid #009879;
}

Vault Secrets

  • Open your workspace → Vault (footer)

{{vault:secret-name}}

Integration with GitHub

  • Home → Integrations
  • Browse All Integrations → Github → Backup a Collection
  • Use a Personal Access Token

Configure to use AWS Cognito

Errors

self signed certificate in certificate chain

  1. File → Settings
  2. General tab → SSL certificate verification = OFF