Postman
開発ツール
Postman
概要
PostmanはAPI開発における業界標準ツールです。APIテスト、モック作成、ドキュメント生成、チーム協働など包括的なAPI開発環境を提供します。
詳細
Postman(ポストマン)は2012年にSiddharthaPuriによって最初のChrome拡張機能として開発が始まり、現在では世界で最も使用されているAPI開発プラットフォームです。シンプルなHTTPリクエスト送信から始まった機能は、現在では企業レベルのAPI開発ライフサイクル全体をサポートする包括的なプラットフォームに進化しています。直感的なGUIによりAPIエンドポイントの作成、テスト、文書化が可能で、複雑なワークフローも視覚的に管理できます。コレクション機能によりAPIリクエストをグループ化でき、環境変数により異なる開発段階での設定管理も効率化されています。また、モック機能によりAPIが実装される前から開発を進めることができ、自動テスト機能により継続的な品質保証も実現できます。チーム機能では複数人でのコラボレーションが可能で、API設計からテスト、デプロイメントまでの全工程を統合管理できます。
メリット・デメリット
メリット
- 直感的なインターフェース: 技術者でなくても使いやすいGUI設計
- 包括的な機能: テスト、モック、文書化、監視まで一元化
- チーム協働: コレクション共有とリアルタイムコラボレーション
- 豊富な認証サポート: OAuth、JWT、Basic認証など幅広い対応
- 環境管理: 開発・テスト・本番環境の切り替えが容易
- 自動テスト: CI/CDパイプラインとの統合によるテスト自動化
- 豊富なエコシステム: プラグインとAPIによる拡張性
デメリット
- 有料プラン: 高度な機能やチーム利用には課金が必要
- データプライバシー: クラウド上でのデータ保存に関する懸念
- 学習コスト: 高度な機能を活用するには時間が必要
- パフォーマンス: 大量のリクエスト処理時の動作が重い場合
- オフライン制限: 一部機能でインターネット接続が必要
主要リンク
- Postman公式サイト
- Postman Learning Center
- Postman API Platform
- Postman Community
- Postman Workspace
- Postman Documentation
書き方の例
基本的なGETリクエスト
// Environment変数の設定例
{
"baseUrl": "https://api.example.com",
"apiKey": "{{API_KEY}}"
}
// GETリクエストのURL例
{{baseUrl}}/users/{{userId}}?api_key={{apiKey}}
// Headers設定
Content-Type: application/json
Authorization: Bearer {{token}}
POSTリクエストでのデータ送信
// Body (raw JSON)
{
"name": "田中太郎",
"email": "[email protected]",
"age": 30,
"department": "engineering"
}
// Pre-request Script例
pm.environment.set("timestamp", Date.now());
pm.environment.set("uuid", pm.variables.replaceIn('{{$guid}}'));
レスポンステストの記述
// Tests タブでのアサーション例
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response time is less than 1000ms", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
pm.test("User created successfully", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.status).to.eql("success");
pm.expect(responseJson.data.id).to.exist;
// 次のリクエストで使用するため変数に保存
pm.environment.set("userId", responseJson.data.id);
});
pm.test("Required fields exist", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.data).to.have.property("name");
pm.expect(responseJson.data).to.have.property("email");
pm.expect(responseJson.data.email).to.include("@");
});
認証の設定例
// OAuth 2.0の設定例
// Authorization タブで設定
Grant Type: Authorization Code
Auth URL: https://auth.example.com/oauth/authorize
Access Token URL: https://auth.example.com/oauth/token
Client ID: {{clientId}}
Client Secret: {{clientSecret}}
Scope: read write
// JWTトークンの前処理
// Pre-request Script
const credentials = {
username: pm.environment.get("username"),
password: pm.environment.get("password")
};
pm.sendRequest({
url: pm.environment.get("authUrl") + "/login",
method: 'POST',
header: {
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify(credentials)
}
}, function (err, response) {
if (err) {
console.error(err);
} else {
const token = response.json().token;
pm.environment.set("jwtToken", token);
}
});
動的データ生成
// Dynamic variables(動的変数)の使用例
{
"id": "{{$guid}}",
"timestamp": "{{$timestamp}}",
"randomString": "{{$randomString}}",
"randomEmail": "{{$randomEmail}}",
"randomFirstName": "{{$randomFirstName}}",
"randomLastName": "{{$randomLastName}}",
"randomInt": "{{$randomInt}}"
}
// カスタム動的データ生成(Pre-request Script)
const faker = require('faker');
pm.environment.set("randomName", faker.name.findName());
pm.environment.set("randomCompany", faker.company.companyName());
pm.environment.set("randomAddress", faker.address.streetAddress());
コレクション実行の自動化
# Newman (CLI版Postman) を使用した自動化例
npm install -g newman
# コレクションの実行
newman run "API Collection.postman_collection.json" \
--environment "Production.postman_environment.json" \
--reporters cli,json \
--reporter-json-export results.json
# CI/CDパイプラインでの使用例(GitHub Actions)
name: API Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Newman
run: npm install -g newman
- name: Run API Tests
run: newman run postman/collection.json -e postman/env.json