1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

postman mock serverの利用方法

Last updated at Posted at 2022-12-04

postman mock serverを利用方法(個人ノート)

Abstract:
Flutter開発はフロントエンドなので、APIテストが必要になる。
そのため、Postmanのmock server機能を活用して、簡単にget/post requestが実現できる。以下の内容はmock serverに基づくrequestの方法を説明する。

手順の詳細:
インストールや新規作成などの手順はホームページに掲載されているため、こちらで省略する。

1.mock serverの立ち上がり:
①server nameとEnvironmentは必須項目です。「新規のEnvironment」をチェックする。
image.png
②Request bodyにチェック入りの場合は、Bodyの情報と一致すれば、serverは情報を返却する。チェック外しの場合は、どんなBodyに対しても情報を返却する。
image.png
②serverのurlをコピーして、Environmens設定の「INITIAL VALUE」に貼り付ける。VARIABLEは「baseUrl」でも、そのまま「url」でも大丈夫です。
image.png

2.get requestについて:
①getはサーバ情報を取得することです。getのテストはブラウザにも検証できる。

3.post requestについて:
①postは様々な情報を送信できるため、リクエストの返却情報はpostmanに確認できるが、ブラウザに検証できない。

4.リクエストの作成
①get/postのパスは「serverのパス」+「リクエスト名」
image.png
②必ず右上の三つの点をクリックして、「Add example」を実行する。中身のparams情報と下の返却Bodyを編集する。
image.png

image.png
(※そうしなければ、mock serverは返却情報を知らないので、リクエストが失敗になる)

③普通のjson結果を返却させたい場合は、「Add example」にjson形式の返却内容を追加していいです。
image.png

④x-www-form-urlencodedの結果を返却したい場合は、
まずはBodyに「"Key":"value"」を入力する。
image.png
その時、Headersに「Content-Type」のVALUEは自動的に「application/x-www-form-urlencoded」になった(なってない場合は手で入力しても大丈夫です)
image.png
次に、「Add example」をしましょう。
Bodyに「"Key":"value"」を入力する。
▲必ず下の返却Bodyにurl形式の結果を記入する。
image.png
③post requestを送信する
Bodyの送信情報を設定する(mock serverがrequest bodyの場合は、必ずexampleの「"Key":"value"」を一致させる)。下記の結果が返却された。
image.png

 

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?