Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PUTとPOSTどっち使う?

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 PUTとPOSTどっち使う?

Avatar for Hank Ehly

Hank Ehly

June 23, 2022
Tweet

More Decks by Hank Ehly

Other Decks in Technology

Transcript

  1. 自己紹介 • Hank Ehly (ハンク イーリー) • ENECHANGE株式会社 • qiita.com/hankehly

    • connpass.com/user/hankehly • github.com/hankehly • speakerdeck.com/hankehly qiita.com/hankehly > PUTとPOSTどっち使う?
  2. • リクエストに同封されているデータの意図 The fundamental difference between the POST and PUT

    methods is highlighted by the different intent for the enclosed representation (RFC7231) • 使い分けることで、表現力の高い、理解しやすい Web API が作れる PUTとPOSTの違い qiita.com/hankehly > PUTとPOSTどっち使う?
  3. POST • 受け取ったデータで何してもおかしくない • 汎用的 リクエスト レスポンス DBレコードを INSERT する

    201 (Created) ファイルを削除する 204 (No Content) 非同期処理を開始する 202 (Accepted) キャッシュされたリソースにリダイレクトする 303 (See Other) qiita.com/hankehly > PUTとPOSTどっち使う? • 一般的に冪等性がないため、キャッシュの対象にならない
  4. POST /articles HTTP/1.1 { "name": "foo", "author": "tanaka", "content": "hello

    world" } HTTP/1.1 201 Created Location: http://example.com/articles/12 POST • リソースの新規作成に使うことが多い qiita.com/hankehly > PUTとPOSTどっち使う?
  5. POST • リソースの新規作成に使うことが多い POST /articles HTTP/1.1 { "name": "foo", "author":

    "tanaka", "content": "hello world" } HTTP/1.1 201 Created Location: http://example.com/articles/12 qiita.com/hankehly > PUTとPOSTどっち使う?
  6. POST • リソースの新規作成に使うことが多い POST /articles HTTP/1.1 { "name": "foo", "author":

    "tanaka", "content": "hello world" } HTTP/1.1 201 Created Location: http://example.com/articles/12 qiita.com/hankehly > PUTとPOSTどっち使う?
  7. PUT

  8. PUT INSERT INTO articles (id, name, author) VALUES (12, "foo",

    "tanaka") ON CONFLICT (id) DO UPDATE SET name = "foo", author = "tanaka" PUTリクエストをSQLに例えたら qiita.com/hankehly > PUTとPOSTどっち使う?
  9. PUT INSERT INTO articles (id, name, author) VALUES (12, "foo",

    "tanaka") ON CONFLICT (id) DO UPDATE SET name = "foo", author = "tanaka" PUT /articles/12 HTTP/1.1 { "name": "foo", "author": "tanaka" } PUTリクエストをSQLに例えたら qiita.com/hankehly > PUTとPOSTどっち使う?