TIL51. HTTP : HTTP Method

ID์งฑ์žฌยท2021๋…„ 10์›” 23์ผ
0

HTTP

๋ชฉ๋ก ๋ณด๊ธฐ
5/17
post-thumbnail
post-custom-banner

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” HTTP์˜ GET, POST, PUT, PATCH, DELETE ๋งค์„œ๋“œ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ HTTP Method

๐Ÿ”ฅ HTTP ๋งค์„œ๋“œ๋ž€?

๐Ÿ”ฅ GET, POST ๋งค์„œ๋“œ

๐Ÿ”ฅ PUT, PATCH, DELETE ๋งค์„œ๋“œ

๐Ÿ”ฅ HTTP ๋งค์„œ๋“œ์˜ ์†์„ฑ



1. HTTP ๋งค์„œ๋“œ๋ž€?

๐Ÿค” HTTP ๋งค์„œ๋“œ๋Š” ์™œ ํ•„์š”ํ• ๊นŒ?

โœ”๏ธ ์ž์›(resource) ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด URL์„ ์„ค๊ณ„ํ•  ๋•Œ, URL์€ ์ž์›์— ํ•ด๋‹นํ•˜๋Š” ๊ทธ ๋Œ€์ƒ์— ๋Œ€ํ•ด์„œ๋งŒ ์‹๋ณ„ํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋˜๊ณ , ์ž์›์˜ ํ–‰์œ„์— ๋Œ€ํ•ด์„œ๋Š” ๋ถ„๋ฆฌํ•ด์•ผํ•œ๋‹ค.

โœ”๏ธ ์˜ˆ๋ฅผ๋“ค์–ด, ํšŒ์› ๋“ฑ๋ก, ํšŒ์› ์กฐํšŒ, ํšŒ์› ์ˆ˜์ •, ํšŒ์› ์‚ญ์ œ URL์„ ๋งŒ๋“ค ๋•Œ, ์‹๋ณ„ํ•ด์•ผํ•˜๋Š” ์ž์›์€ ํšŒ์›์ด๋‹ค.

โœ”๏ธ ๋”ฐ๋ผ์„œ ๋“ฑ๋ก, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ๋Š” ์ž์›์— ๋Œ€ํ•œ ํ–‰์œ„์ด๊ธฐ ๋•Œ๋ฌธ์— HTTP ๋งค์„œ๋“œ๋ฅผ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•ด์„œ API URL์„ ์„ค๊ณ„ํ•ด์•ผ ํ•œ๋‹ค.

โœ”๏ธ ์ฆ‰, HTTP ๋งค์„œ๋“œ๊ฐ€ ํ•„์š”ํ•œ ์ด์œ ๋Š” URL์—์„œ ์ž์›์˜ ๋Œ€์ƒ๋งŒ์„ ์‹๋ณ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ ๋“ฑ์ด ๋ชจ๋‘ ๊ฐ™์€ URL์„ ๊ฐ€์ง€์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ HTTP ๋งค์„œ๋“œ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ์ด๋‹ค.

๐Ÿค” HTTP ๋งค์„œ๋“œ๋Š” ๋ฌด์—‡์ด ์žˆ์„๊นŒ?

โœ”๏ธ ์ฃผ์š” HTTP ๋งค์„œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

โœ”๏ธ ์ž์›(resource)์ด๋ž€ ์šฉ์–ด ๋Œ€์‹  ํ‘œ์ƒ(representation)์ด๋ž€ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. ํ–‰์œ„๊นŒ์ง€ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  • GET : ํ‘œ์ƒ(representation) ์กฐํšŒ ๐Ÿ‘ˆ READ
  • POST : ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ์ฃผ๋กœ ํ‘œ์ƒ(representation) ๋“ฑ๋ก์— ์‚ฌ์šฉ ๐Ÿ‘ˆ CREATE
  • PUT : ์—†์œผ๋ฉด ํ‘œ์ƒ(representation)์„ ์ƒ์„ฑ, ์žˆ์œผ๋ฉด ํ˜„์žฌ ์ž์›(representation)์œผ๋กœ ๋Œ€์ฒด ๐Ÿ‘ˆ UPDATE
  • PATCH : ํ‘œ์ƒ(representation) ๋ถ€๋ถ„ ๋ณ€๊ฒฝ ๐Ÿ‘ˆ UPDATE
  • DELETE : ํ‘œ์ƒ(representation) ์‚ญ์ œ ๐Ÿ‘ˆ DELETE
  • HEAD : GET๊ณผ ๋™์ผํ•˜์ง€๋งŒ ๋ฉ”์‹œ์ง€ ๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ , header๊นŒ์ง€๋งŒ ๋ฐ˜ํ™˜


2. GET, POST ๋งค์„œ๋“œ

๐Ÿค” GET ๋งค์„œ๋“œ

โœ”๏ธ GET์€ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

โœ”๏ธ GET ๋งค์„œ๋“œ๋„ Body๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ์ผ๋ฐ˜์ ์œผ๋กœ Body๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ query๋ฅผ ํ†ตํ•ด ์ „๋‹ฌํ•œ๋‹ค.

โœ”๏ธ GET์š”์ฒญ์„ ๋ฐ›์€ ์„œ๋ฒ„๋Š” DB๋ฅผ ํ™•์ธํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ํด๋ผ์ด์–ธํŠธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์‘๋‹ตํ•œ๋‹ค.

๐Ÿค” POST ๋งค์„œ๋“œ

โœ”๏ธ POST๋Š” body ์˜์—ญ์— ๋ฐ์ดํ„ฐ์— ๋‹ด์•„ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ดํ•œ๋‹ค.

โœ”๏ธ ์ผ๋ฐ˜์ ์œผ๋กœ query์— ๋ณด์—ฌ์ ธ์„œ๋Š” ์•ˆ๋˜๋Š” ์ค‘์š”ํ•œ ์ •๋ณด๋Š” body์— ๋‹ด์•„ POSTํ˜•์‹์œผ๋กœ ์š”์ฒญ๋œ๋‹ค.

โœ”๏ธ ์‹ค์ œ๋กœ POST๋งŒ์œผ๋กœ๋„ ๋ชจ๋“  ํ–‰์œ„์— ๋Œ€ํ•ด ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹ค๋งŒ, POST๋Š” ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ๋ฐ ํ”„๋กœ์„ธ์Šค ์ฒ˜๋ฆฌ์— ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋œ๋‹ค.

โœ”๏ธ ์˜ˆ๋ฅผ๋“ค์–ด ์กฐํšŒํ•  ๋•Œ, POST๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด cashingํ•˜๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ์กฐํšŒ์— ์žˆ์–ด์„œ๋Š” GET์ด ๋” ์œ ๋ฆฌํ•˜๋‹ค.

โœ”๏ธ "/members/" URI์˜ POST์˜ ์š”์ฒญ์ด ๊ณ„์ • ์ƒ์„ฑ์œผ๋กœ ๋ฏธ๋ฆฌ ์•ฝ์†๋˜์–ด ์žˆ๋‹ค๋ฉด, ๊ณ„์ •์„ ์ƒ์„ฑํ•œ ๋’ค ์•„๋ž˜์™€ ๊ฐ™์ด ์‘๋‹ต๋ฉ”์‹œ์ง€๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „๋‹ฌํ•œ๋‹ค. location์€ ์ž์›์ด ์ƒ์„ฑ๋œ ๊ฒฝ๋กœ๋‹ค.

โœ”๏ธ POST๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ์š”์ฒญํ•  ๋•Œ, path๋งŒ ์ง€์ •ํ•˜๊ณ  ์ž์›(resource)์˜ ์œ„์น˜๋Š” ์ง€์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํด๋ผ์ด์–ธํŠธ์—์„œ ์ž์›์ด ์ƒ์„ค๋  URL(id๊ฐ’)์„ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด์— ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด ๋ฆฌ์†Œ์Šค URL๋ฅผ ์ƒ์„ฑํ•ด์„œ location๊ฐ’์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•ด์ค€๋‹ค. ์ด๋ฅผ ์ปฌ๋ ‰์…˜(Collection)์ด๋ผ ํ•œ๋‹ค.



3. PUT, PATCH, DELETE ๋งค์„œ๋“œ

๐Ÿค” PUT ๋งค์„œ๋“œ

โœ”๏ธ PUT์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ์™„์ „ํžˆ ๋Œ€์ฒดํ•˜๊ณ  ์—†์œผ๋ฉด ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์น˜ ํด๋”์— ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์œผ๋ฉด ๋ฎ์–ด์“ฐ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.

โœ”๏ธ ๋‹จ, PUT์€ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•  ๋•Œ, ์ž์›(resource) ์œ„์น˜๋ฅผ ์ด๋ฏธ ์‹๋ณ„ํ•˜๊ณ  URI๋ฅผ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์ด PUT๊ณผ POST๋Š” ์ฐจ์ด๋‹ค.

โœ”๏ธ PUT์—์„œ ์ž์›(resource)์˜ ์œ„์น˜๋ฅผ ์ง์ ‘ ์ง€์ •ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์— ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, ํด๋ผ์ด์–ธํŠธ์—์„œ ์ž์›(resource)์˜ ์œ„์น˜(URL)๋ฅผ ์ด๋ฏธ ์•Œ๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ฐœ๋…์„ ์Šคํ† ์–ด(Store)๋ผ ํ•œ๋‹ค.

๐Ÿค” PATCH ๋งค์„œ๋“œ

โœ”๏ธ PATCH๋Š” ์ž์›(resource)์„ ๋ถ€๋ถ„ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋‹น์—ฐํžˆ ์ž์›์˜ ์œ„์น˜๋ฅผ ๋ช…ํ™•ํžˆ ์•Œ์•„์•ผ ํ•œ๋‹ค.

โœ”๏ธ ๋‹จ, ์„œ๋ฒ„์—์„œ PATCH ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” POST๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿค” DELETE ๋งค์„œ๋“œ

โœ”๏ธ DELETE๋Š” ์ž์›(resource)๋ฅผ ์‚ญ์ œํ•œ๋‹ค. ๋‹น์—ฐํžˆ ์ž์›์˜ ์œ„์น˜๋ฅผ ๋ช…ํ™•ํžˆ ์•Œ์•„์•ผ ํ•œ๋‹ค.



4. HTTP ๋งค์„œ๋“œ์˜ ์†์„ฑ

๐Ÿค” ์•ˆ์ „(Safe Methods)

โœ”๏ธ ์•ˆ์ „์ด๋ž€ ๋งค์„œ๋“œ ํ˜ธ์ถœ์ด ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณ€๊ฒฝ์„ ์œ ๋ฐœํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. GET์€ ์กฐํšŒ๋งŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ „ํ•˜์ง€๋งŒ, POST, PUT, PATCH, DELETE๋Š” ์•ˆ์ „ํ•˜์ง€ ์•Š๋‹ค.

โœ”๏ธ ๋‹จ, ์•ˆ์ „์ด๋ž€ ํ•ด๋‹น ์ž์›(resource)์ด ๋ณ€ํ•˜๋ƒ ๋ณ€ํ•˜์ง€ ์•Š๋ƒ๋งŒ์„ ๊ณ ๋ คํ•œ ์˜๋ฏธ๋‹ค. ์ฆ‰, ๊ณ„์† ํ˜ธ์ถœํ•ด์„œ ์„œ๋ฒ„์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹คํ•ด๋„ ์ด๋Š” ์•ˆ์ „์ด๋ž€ ํŠน์ง•๊ณผ๋Š” ๋ฌด๊ด€ํ•ฉ๋‹ˆ๋‹ค. ์ž์›๋งŒ ์ž์ฒด์— ์ฃผ๋Š” ์˜ํ–ฅ๋งŒ ๊ณ ๋ คํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๐Ÿค” ๋ฉฑ๋“ฑ(Idempotent Methods)

โœ”๏ธ ๋ฉฑ๋“ฑ์€ ํ•œ ๋ฒˆ ํ˜ธ์ถœํ•˜๋“  ๋‘๋ฒˆ ํ˜ธ์ถœํ•˜๋˜ 100๋ฒˆ ํ˜ธ์ถœํ•˜๋˜ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•œ ์‹ ๋ขฐ๋„์˜ ๊ฐœ๋…์ด๋‹ค.

โœ”๏ธ ์ฆ‰, ๋™์ผํ•œ ๋ฐ์ดํ„ฐ์— GET, PUT, DELETE๋ฅผ ํ•œ๋ฒˆํ•˜๋˜ 100๋ฒˆํ•˜๋˜ ์กฐํšŒ, ๋ฎ์–ด์”Œ์šฐ๊ธฐ, ์‚ญ์ œ์˜ ๊ฒฐ๊ณผ๋Š” ๋ณ€๋™์ด ์—†๋‹ค. ์ด์— GET, PUT, DELETE๋Š” ๋ฉฑ๋“ฑํ•˜๋‹ค.

โœ”๏ธ ์ด์— ๋ฐ˜ํ•ด POST๋Š” ๋‘ ๋ฒˆ ํ˜ธ์ถœํ•˜๋ฉด ์ค‘๋ณตํ•ด์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฉฑ๋“ฑํ•˜์ง€ ์•Š๋‹ค๊ณ  ํ‘œํ˜„ํ•œ๋‹ค.

๐Ÿค” ์บ์‹œ ๊ฐ€๋Šฅ(Cacheable Methods)

โœ”๏ธ ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ ์บ์‰ฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋ฅผ ์บ์‰ฌ ๊ฐ€๋Šฅ์ด๋ผ ํ•œ๋‹ค.
โœ”๏ธ GET, HEAD, POST, PATCH๋Š” ์บ์‹œ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” GET, HEAD ์ •๋„๋งŒ ์บ์‹œ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

profile
Keep Going, Keep Coding!
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€