LoginSignup
0
0

More than 1 year has passed since last update.

Microsoft Azure - API Management Service: 固定のレスポンスを返す

Posted at

Azure の API Management Service で、 固定のレスポンスを (サーバを作らずに) 返す方法を紹介します。

Frontend と Inbound の設定を行うことで、可能になります。

image.png

STEP 1

Mock としてのレスポンスを有効にします。
これは、Inbound のポリシーを使います。

"Add policy" をクリックして、GUIで設定可能です。

image.png

Add policy をクリックすると、追加可能ないくつかのポリシーが表示されます。 そのなかから Mock responses を選びます。

image.png

選択後は、レスポンスステータスとレスポンスのmime type入力する画面になります。 "200 OK, text/plain" にしました。

image.png

これを XML を使って記述すると下のようになります。

<policies>
    <inbound>
        <base />
        <mock-response status-code="200" content-type="text/plain" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

この時点でAPIへリクエストを行うと、BODYのないレスポンスが返ってきます。

STEP 2

Frontend の設定で、先ほど設定したレスポンスにマッチするBODYを記述します。
ステータスは 200、 Content-Type は text/plain でしたから、それに合わせたレスポンスを設定します。 "sample" という文字列を返すように設定しました。

image.png

これでBODYを含む固定のレスポンスが返るようになりました。

Content-Type を application/json に設定していれば 決まった JSON を返すこともできます。

テスト

上で設定したAPIへリクエストを送ってみました。
期待通りの結果が返ってきました。

% curl -i -X GET \
 'https://xxx.azure-api.net/test4'
HTTP/1.1 200 OK
Content-Length: 6
Content-Type: text/plain
Date: Tue, 03 Jan 2023 13:44:14 GMT

sample
0
0
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
0
0