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
pm.request.headers.add({ key: 'header_name', value: 'header_value' }) |
Environment
Permet de définir des variables par environnement.
- Ajouter des environnement: Engrenage à droite
- Changer d'environnement: liste déroulante "No environment"
Variable: {{MyVar}}
Import
Swagger
- 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
- 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 (en haut à droite)
Tests
Dans une requête → onglet Tests
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
// 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:
- Pre-request script
- Request
- Response
- Test
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
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}}
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
// get the items from the data file const items = data.items; // get randomly one of the item var item = _.sample(items); |
Visualizer
// 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() }); |
.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; } |
Configure to use AWS Cognito
Errors
self signed certificate in certificate chain
- File → Settings
- General tab → SSL certificate verification = OFF