TL;DR
Quoi : Plateforme de développement d’API pour construire, tester et documenter les APIs.
Pourquoi : Interface visuelle, collections, environnements, tests automatisés, collaboration d’équipe.
Quick Start
Installer : Télécharger depuis postman.com/downloads
Première requête :
- Cliquez sur New → Request
- Entrez l’URL :
https://jsonplaceholder.typicode.com/posts/1 - Cliquez sur Send
- Voir la réponse dans le panneau inférieur
Créer une collection :
- Cliquez sur Collections → Create Collection
- Nommez-la “My API”
- Ajoutez des requêtes pour organiser
Cheatsheet
| Fonctionnalité | Description |
|---|---|
| Collections | Organiser les requêtes |
| Environments | Ensembles de variables (dev/prod) |
| Variables | Syntaxe {{variable}} |
| Pre-request Script | Exécuter avant la requête |
| Tests | Valider les réponses |
| Runner | Exécution par lots |
Gotchas
Variables d’environnement
// Set in environment
// BASE_URL: https://api.example.com
// TOKEN: your-auth-token
// Use in request URL
// {{BASE_URL}}/users
// Use in headers
// Authorization: Bearer {{TOKEN}}
Script de pré-requête
// Generate timestamp
pm.environment.set("timestamp", Date.now());
// Generate random data
pm.environment.set("randomEmail", `user${Math.random().toString(36).slice(2)}@test.com`);
// Get data from previous request
const token = pm.environment.get("authToken");
pm.request.headers.add({
key: "Authorization",
value: `Bearer ${token}`
});
Scripts de test
// Check status
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Check response time
pm.test("Response time < 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
// Check response body
pm.test("Has user id", function () {
const json = pm.response.json();
pm.expect(json).to.have.property("id");
pm.expect(json.id).to.be.a("number");
});
// Save to environment
const json = pm.response.json();
pm.environment.set("userId", json.id);
// Check array
pm.test("Returns array of users", function () {
const json = pm.response.json();
pm.expect(json).to.be.an("array");
pm.expect(json.length).to.be.greaterThan(0);
});
Collection Runner
// Run collection with data file (CSV/JSON)
// data.json:
[
{ "email": "[email protected]", "name": "User 1" },
{ "email": "[email protected]", "name": "User 2" }
]
// In request body
{
"email": "{{email}}",
"name": "{{name}}"
}
// Access iteration data in tests
pm.test("User created", function () {
const json = pm.response.json();
pm.expect(json.email).to.eql(pm.iterationData.get("email"));
});
Newman CLI
# Install
npm install -g newman
# Run collection
newman run collection.json
# With environment
newman run collection.json -e environment.json
# Generate report
newman run collection.json -r html
Next Steps
- Postman Learning Center - Tutoriels
- Postman API - API Postman
- Newman - CLI Runner
- Postman Flows - Workflows visuels