astech

柱に印をつける身長記録の、デジタル版

REST APIのスキーマ設計直してもらったメモ

BAD

Create

POST http://path.to.backend/api/v1/follow/

{
  target: string,
}

Delete

DELETE http://path.to.backend/api/v1/follow/

{
  target: string,
}

GOOD

Create

PUT http://domain/api/v1/followings/<target>

Delete

DELETE http://domain/api/v1/followings/<target>

変更のお気持ちは?

  • リソースの場所が確定しているか?
    • リソースの場所が確定しているならURLに含める
    • リソースの場所が確定していないならbodyに含める

新しい記事を投稿する場合

リソース(記事)のIDは確定していない

POST http://domain/api/v1/post

{
  title: "hoge"
  body: "fugafuga"

}

id=35の記事を削除する場合

リソース(記事)のIDは確定している!!

DELETE http://domain/api/v1/post/35

参考

https://www.amazon.co.jp/Web%E3%82%92%E6%94%AF%E3%81%88%E3%82%8B%E6%8A%80%E8%A1%93-HTTP%E3%80%81URI%E3%80%81HTML%E3%80%81%E3%81%9D%E3%81%97%E3%81%A6REST-WEB-PRESS-plus/dp/4774142042