てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible/AAP] Automation Controller の API エンドポイントは末尾のスラッシュ(trailing slash)を付けるのが吉

はじめに

タイトルがほぼ全てです。

Automation Controlle の API エンドポイントには、末尾のスラッシュ(trailing slash)があります。

developers.redhat.com

ドキュメント通り末尾のスラッシュを付けるべきですが、付けなくて問題なく見えることがあるので、うっかり忘れてしまうかもしれません。

末尾のスラッシュを付けなくても問題なく見えるパターン

試した限り GET のパターンです。

  • GET で /api/v2/ping をリクエストする
  • 301 が返ってくる
  • GET で /api/v2/ping/ をリクエストしなおす

試せていませんが、DjangoAPPEND_SLASH という設定による(デフォルト True)のではないかと思います。

末尾のスラッシュを付けないとまずいケース

GET 以外、たとえば POST をするケースです。

  • POST で /api/v2/job_templates/99/launch をリクエストする
  • 301 が返ってくる
  • GET/api/v2/job_templates/99/launch/ をリクエストしなおす
  • GET の結果が返ってくる

リクエスト時に元のメソッドを維持するかどうかは、クライアント側の設定次第のようですが、GET になることが結構多いようです。

Postman での挙動

デフォルトでは前述の通り、GET でリクエストし直していました。

Settingの Follow original HTTP Method を有効にすると、 POST のままにしてくれました。(Automatically follow redirects はデフォルト有効のまま)

Follow original HTTP Method

curl での挙動

curl-L, --location オプションには以下の説明がありました。

              When curl follows a redirect and if the request is a POST, it sends the following request with a GET if the HTTP
              response was 301, 302, or 303. If the response code was any other 3xx code, curl resends the following request
              using the same unmodified method.

              You can tell curl to not change POST requests to GET after a 30x response by using the dedicated options for
              that: --post301, --post302 and --post303.

ただ、

curl --location -X POST 'https://192.168.1.99/api/v2/job_templates/99/launch' --header 'Authorization: Basic xxxxxxxxxx' -k -v

のように、POST で trailing slash なし(body もなし)で確認すると、以下のように POST のままリクエストしていました。なにか気の利いた機能が効いてるのかもしれません。

> POST /api/v2/job_templates/99/launch HTTP/1.1
...(略)...
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Fri, 07 Jun 2024 12:27:31 GMT
< Content-Type: text/html
< Content-Length: 162
< Location: https://192.168.1.99/api/v2/job_templates/99/launch/
...(略)...
< 
* Ignoring the response-body
* Connection #0 to host 192.168.1.99 left intact
* Issue another request to this URL: 'https://192.168.1.99/api/v2/job_templates/99/launch/'
* Found bundle for host: 0x7fc96f106460 [serially]
* Can not multiplex, even if we wanted to
* Re-using existing connection with host 192.168.1.99
> POST /api/v2/job_templates/56/launch/ HTTP/1.1
...(略)...
> 
< HTTP/1.1 201 Created

まとめ

ドキュメント通り、末尾のスラッシュ(trailing slash)を付けるのが吉です。

参考

qiita.com

docs.djangoproject.com

qiita.com