« Postman » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
(22 versions intermédiaires par le même utilisateur non affichées)
Ligne 2 : Ligne 2 :
= Preset Headers =
= Preset Headers =
Permet de définir des headers par défaut.
Permet de définir des headers par défaut.
* Headers → presets (à droite)
* YourQuery → Headers tab → presets (on the right)
** Ajouter des presets: Manage presets
** Create a new preset: Manage presets
** Charger un preset: {PresetName}
** Apply a preset: {PresetName}
 
= [https://community.postman.com/t/setting-headers-for-entire-collection-folder/708/13 Set headers for a collection] =
In the collection → Pre-request Script
<kode lang='js'>
pm.request.headers.add({ key: 'header_name', value: 'header_value' })
</kode>
 
= Workspace =
Use 1 workspace by application.


= Environment =
= Environment =
Permet de définir des variables par environnement.
Allow to set variables by environnement: {{boxx|local}} {{boxx|dev}} {{boxx|prod}}.<br>
* Ajouter des environnement: Engrenage à droite
* Changer d'environnement: liste déroulante "No environment"
 
Variable: {{boxx|<nowiki>{{MyVar}}</nowiki>}}
Variable: {{boxx|<nowiki>{{MyVar}}</nowiki>}}


= Import =
= Import =
== Swagger ==
=== Restricted access Swagger UI ===
# From Swagger UI web page - below the title there is a link to the JSON version
# Open it, then save it
# From Postman - Import - select the downloaded JSON file
== Query ==
Permet d'importer une requête copiée le navigateur
Permet d'importer une requête copiée le navigateur
* firefox → developer tools → network
* firefox → developer tools → network
Ligne 22 : Ligne 35 :
= Proxy =
= Proxy =
Permet de capturer le traffic.
Permet de capturer le traffic.
* Icone antenne (en haut à droite)
* Icone antenne (footer en bas à droite)


= Tests =
= Tests =
Ligne 129 : Ligne 142 :
});
});
</kode>
</kode>
Authorization → Type = {{boxx|BearerToken}} → Token = {{boxx|<nowiki>{{accessToken}}</nowiki>}}
=  Auto-refresh token =
If the {{boxx|Auto-refresh token}} button is greyed out, it is because you didn't fetch a refresh token.
* on [https://community.postman.com/t/auto-refresh-token-is-not-working/43367/8 Azure], you have to add {{boxx|offline_access}} in the {{boxx|Scope}}


= Collections =
= Collections =
Ligne 146 : Ligne 165 :
var item = _.sample(items);
var item = _.sample(items);
</kode>
</kode>
= [https://learning.postman.com/docs/sending-requests/visualizer Visualizer] =
<kode lang='js'>
// 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()
});
</kode>
<kode lang='css' collapsed>
.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;
}
</kode>
= [https://learning.postman.com/docs/sending-requests/postman-vault/postman-vault-secrets Vault Secrets] =
* Open your workspace → Vault (footer)
{{boxx|<nowiki>{{vault:secret-name}}</nowiki>}}
{{warn | Integration with AWS is available on Postman Enterprise plans with the Advanced Security Administration add-on.}}
= Integration with GitHub =
* Home → Integrations
* Browse All Integrations → Github → Backup a Collection
* Use a [[Github#Personal_Access_Token|Personal Access Token]]
= [https://rcarrillo.dev/connecting-postman-to-amazon-cognito-using-the-id_token/ Configure to use AWS Cognito]=


= Errors =
= Errors =

Dernière version du 18 novembre 2024 à 14:51

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 AWS is available on Postman Enterprise plans with the Advanced Security Administration add-on.

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