はじめに
NewmanとはPostmanのCLIツールです。
何が困ったか
以下のことをしたかったのですが2つ目の処理でCookieを認識できず401になっていました。
- ログインAPIへリクエスト(認証後のtokenをsetCookie)
- ログイン後の処理APIへリクエスト(Cookieにtokenがない場合401にする)
Postmanだと
Cookiesに全て格納されるので、今回のようにリクエストが分かれていても特別意識せずに2回リクエストの際はCookieが参照可能な状態です(セキュリティ的な制限などなければ)
Newmanだと
以下のように2つ目のAPIを実行した際Cookieを参照できません。
なお、newmanは公式のimageファイルのDockerfileからCMDを削除して作成したコンテナで動かしていますので、docker exec newman(コンテナ名)
が先頭にあります。
# Loginは成功
$ docker exec newman newman run "collections/udemy-echo-collection.json" -e "environments/udemy-echo-dev-environment.json" --folder "Login"
→ Login
POST http://go-rest-api:8080/login [200 OK, 288B, 290ms]
# Login後のCookie情報が取得できない
$ docker exec newman newman run "collections/udemy-echo-collection.json" -e "environments/udemy-echo-dev-environment.json" --folder "Tasks"
→ Tasks
GET http://go-rest-api:8080/tasks [401 Unauthorized, 172B, 126ms]
対策
2つあります。
どちらもコンテナでの動作確認になります。
docker exec newman(コンテナ名)
コマンドを外せば、コンテナでなくても対応可能と思いますが動作確認はしていません。
2つの処理を1つのcollectionファイルに記載し、folderを指定しないで実行
jsonファイルにどちらも記載します。
{
"info": {
"_postman_id": "",
"name": "Echo CRUD",
"description": "### Intro\n\nFollow along with this GitHub: **[https://github.com/c3drive/my_tools/tree/main/newman]()**.\n\n* The [Echo CRUD API Documentaion](https://github.com/c3drive/my_web_scraping/tree/main/api_server/go-rest-api)\n* The [Udemy course](https://www.udemy.com/course/echo-go-react-restapi/).\n\n### Get Started\n\n[Update the environment](https://github.com/c3drive/my_tools/tree/main/newman/environments/udemy-echo-dev-environment) with your own authorization credentials.\n\n| Required Environment Variables | Description |\n|------------------------|-------------|\n| `host_name` | request host. |\n| `xxx` | xxx. |\n| `xxxxx` | xxx. |\n| `xxx` | xxx. |\n| `xxx` | xxx. |\n\n### Run the collection\n\nThis operational collection register. \n\n* **Controlling your workflow:** xxxxx.\n* **Saving state:xxxxxxx.\n\n",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Login",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": {
"email": "{{email}}",
"password": "{{password}}"
},
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{host_name}}/login",
"protocol": "http",
"host": [
"{{host_name}}"
],
"path": [
"login"
]
},
"description": "Set cookie.\n\n"
},
"response": []
},
{
"name": "Tasks",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{host_name}}/tasks",
"protocol": "http",
"host": [
"{{host_name}}"
],
"path": [
"tasks"
]
},
"description": "Get All Tasks.\n\n"
},
"response": []
}
]
}
作成したcollectionファイルをfolderを指定せず実行します。
$ docker exec newman newman run "collections/udemy-login-collection.json" -e "environments/udemy-echo-dev-environment.json"
→ Login
POST http://go-rest-api:8080/login [200 OK, 288B, 257ms]
→ Tasks
GET http://go-rest-api:8080/tasks [200 OK, 237B, 42ms]
2つ目の処理が成功しました。
cookie.jarを利用する
もう一つは、cookie.jarを経由する方法です。
# 保存する
$ docker exec newman newman run "collections/udemy-echo-collection.json" -e "environments/udemy-echo-dev-environment.json" --folder "Login" --export-cookie-jar cookies/login.jar
→ Login
POST http://go-rest-api:8080/login [200 OK, 288B, 210ms]
# 使う(collectionファイルは、保存時と同一でなくても利用可能です)
docker exec newman newman run "collections/udemy-login-collection.json" -e "environments/udemy-echo-dev-environment.json" --folder "Tasks" --cookie-jar cookies/login.jar
→ Tasks
GET http://go-rest-api:8080/tasks [200 OK, 237B, 268ms]
こちらはログイン情報を複数の処理で何度か使う場合や別のコレクションで扱いたい場合、有効なので特別な理由がない限りjarを使うのが良いと思います。
おわりに
cookie以外にもNewmanは意外につまずく点が多く、PostmanがよいGUIツールだと再認識してしまいました。。
ここまで読んでいただきありがとうございました。