REST APIにおける複数削除の設計をどうするか
検索して一番上に出る記事
質問者の案
DELETE /path/abc?itemId=1&itemId=2&itemId=3
POST /path/abc with body: {"ids": [1, 2, 3]}
回答者の案
DELETE /path/abc with body: {"ids": [1, 2, 3]}
複数の対象を消す場合、 DELETE method で request body を使ってよいのか?
ここでは最終的に「allow, but discourge」された
In other cases where the HTTP spec is vague (such as GET, HEAD and DELETE), requestBody is permitted but does not have well-defined semantics and SHOULD be avoided if possible.
ここでは最終的に使わせない方針になった
A client SHOULD NOT generate a body in a DELETE request. A payload
received in a DELETE request has no defined semantics, cannot alter the
meaning or target of the request, and might lead some implementations to
reject the request.
実際にデフォルトでDELETEのrequest bodyを弾く実装があったらしい
↑ で引用されてた
DELETE /articles/1/relationships/comments with body: {"data: [{ "type": "comments", "id": "12" }, { "type": "comments", "id": "13" }]} を許している
Note: RFC 7231 specifies that a DELETE request may include a body, but that a server may reject the request. This spec defines the semantics of a server, and we are defining its semantics for JSON:API.
サーバーもこちらが実装するんだからいいでしょうという感じ?
代替案は?
1件ずつリクエスト
Mastodon とか
これはクライアント側の実装が自由で、かつ1件の削除コストがまあまあ高い (1件ごとに連合への削除リクエストがある) ために合理的
複数に対するグループを作成しておき、列挙せずに URL で指定できるようにする
全件なら DELETE /path/abc/all とか
POST で URL に action を指定
POST /path/abc?action=delete with body: {"ids": []}
POST /path/abc:batchDelete with body
POST などの method でかつ request body の内容で削除と明示
POST /path/abc with body: {"action": "delete", "ids": [1,2,3]}
POST /path/abc with body: {"deleteIds": [1,2,3]}
IBM は /path/abc/bulk with body: {"deletes": ["ID1","ID2","ID3"]} PATCH とかでもいいかも
POST で色々使える Batch Request を用意
Content-Type: multipart/mixed で、 各要素は Content-Type: application/http で中に request header + body
他参考
RESTfulではないのでドキュメントを充実に書くべき