ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

BackEnd

HTTP ๋ฉ”์„œ๋“œ

mokhs 2021. 5. 28. 23:03

๊น€์˜ํ•œ๋‹˜์˜ ๋ชจ๋“  ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ HTTP ์›น ๊ธฐ๋ณธ ์ง€์‹ ๊ฐ•์˜๋ฅผ ์ˆ˜๊ฐ•ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

GET

  • ๋ฆฌ์†Œ์Šค ์กฐํšŒ
  • ์„œ๋ฒ„์— ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ๋Š” query(์ฟผ๋ฆฌ ํŒŒ๋งˆ๋ฆฌํ„ฐ, ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง)์„ ํ†ตํ•ด์„œ ์ „๋‹ฌ
  • ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ณณ์ด ๋งŽ์•„์„œ ๊ถŒ์žฅํ•˜์ง€ ์•Š์Œ
GET /boards?title=hello HTTP/1.1
Host: localhost:8080

POST

  • ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
  • ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋กœ ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
  • ์„œ๋ฒ„๋Š” ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ
    • ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ํ†ตํ•ด ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰
  • ์ฃผ๋กœ ์‹ ๊ทœ ๋ฆฌ์†Œ์Šค ๋“ฑ๋ก, ํ”„๋กœ์„ธ์Šค ์ฒ˜๋ฆฌ ๋“ฑ์— ์‚ฌ์šฉ

์ŠคํŽ™ ์ƒ์˜ POST

POST ๋ฉ”์„œ๋“œ๋Š” ๋Œ€์ƒ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ฆฌ์†Œ์Šค์˜ ๊ณ ์œ ํ•œ ์˜๋ฏธ ์ฒด๊ณ„์— ๋”ฐ๋ผ ์š”์ฒญ์— ํฌํ•จ๋œ ํ‘œํ˜„์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

์œ„ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Œ

  • ์ด ๋ฆฌ์†Œ์Šค URI์— POST์š”์ฒญ์ด ์˜ค๋ฉด ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€ ์ •ํ•ด์ง„ ๊ฒƒ์ด ์—†์Œ
  • ์ฆ‰. ์š”์ฒญ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€ ๋ฆฌ์†Œ์Šค๋งˆ๋‹ค ๋”ฐ๋กœ ์ •ํ•ด์•ผํ•˜๊ณ  ์ด๊ฑด ๊ฐœ๋ฐœ์ž ์ž์œ 
    • ์ƒˆ ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ
    • ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ(๋‚ด๋ถ€ ๋กœ์ง)
    • ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์• ๋งคํ•œ ๊ฒฝ์šฐ
POST /boards HTTP/1.1
Content-Type: application/json

{
    "title": "hello",
  "content": "hi!"
}

PUT

  • ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ๋ฎ์–ด์“ฐ๊ธฐ
  • ๋ฆฌ์†Œ์Šค๋ฅผ ์™„์ „ํžˆ ๋Œ€์ฒดํ•จ
    • ๋ฆฌ์†Œ์Šค๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€์ฒด
    • ๋ฆฌ์†Œ์Šค๊ฐ€ ์—†์œผ๋ฉด ์ƒ์„ฑ
  • ์ค‘์š”! ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฆฌ์†Œ์Šค๋ฅผ ์‹๋ณ„
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฆฌ์†Œ์Šค ์œ„์น˜๋ฅผ ์•Œ๊ณ  URI ์ง€์ •
    • ์ฆ‰. boards/10 ๊ฐ™์ด 10๋ฒˆ ๊ฒŒ์‹œ๊ธ€์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์–ด์•ผํ•จ.
    • ๋ฆฌ์†Œ์Šค ์œ„์น˜๋ฅผ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ POST์™€ ์ฐจ์ด์ ์ด ์žˆ์Œ

๋ฆฌ์†Œ์Šค๋ฅผ ์™„์ „ํžˆ ๋Œ€์ฒด

๊ธฐ์กด์— 10๋ฒˆ ๊ฒŒ์‹œ๊ธ€์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๋‹ค๊ณ  ํ•ด๋ณด์ž

{
    "title" : "hello",
    "content" : "hi"
}

์ด ์ƒํ™ฉ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด PUT ์š”์ฒญ์„ ํ•˜๋ฉด

PUT /boards/10 HTTP/1.1
Content-Type: application/json

{
    "title": "hello",
}

10๋ฒˆ ๊ฒŒ์‹œ๊ธ€์˜ ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์™„์ „ํžˆ ๋Œ€์ฒด๋œ๋‹ค

{
    "title" : "hello",
}

PATCH

  • ๋ฆฌ์†Œ์Šค ๋ถ€๋ถ„ ๋ณ€๊ฒฝ
  • PUT๊ณผ ๋‹ฌ๋ฆฌ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ถ€๋ถ„ ๋ณ€๊ฒฝํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•œ๋‹ค
  • ์•„๋ž˜ ์˜ˆ์‹œ ์ฒ˜๋Ÿผ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๊ธฐ์กด ๋ฐ์ดํ„ฐ ์ค‘ title๋งŒ "hi"๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
  • ์ฃผ์˜! PATCH๊ฐ€ ์ง€์› ์•ˆ๋˜๋Š” ์„œ๋ฒ„๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Œ → POST๋กœ ํ•ด๊ฒฐ
PATCH /boards/10 HTTP/1.1
Content-Type: application/json

{
    "title": "hi",
}

DELETE

  • ๋ฆฌ์†Œ์Šค ์‚ญ์ œ
  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฆฌ์†Œ์Šค ์œ„์น˜๋ฅผ ์•Œ๊ณ  ์žˆ์–ด์•ผํ•จ
DELETE /boards/10 HTTP/1.1
Host: localhost:8080

{
    "title": "hi",
}

๊ธฐํƒ€ ๋ฉ”์†Œ๋“œ

  • HEAD: GET๊ณผ ๋™์ผํ•˜์ง€๋งŒ ๋ฉ”์‹œ์ง€ ๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ , ์ƒํƒœ ์ค„๊ณผ ํ—ค๋”๋งŒ ๋ฐ˜ํ™˜
  • OPTIONS: ๋Œ€์ƒ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํ†ต์‹  ๊ธฐ๋Šฅ ์˜ต์…˜(๋ฉ”์„œ๋“œ)์„ ์„ค๋ช…(์ฃผ๋กœ CORS์—์„œ ์‚ฌ์šฉ)
  • CONNECT: ๋Œ€์ƒ ์ž์›์œผ๋กœ ์‹๋ณ„๋˜๋Š” ์„œ๋ฒ„์— ๋Œ€ํ•œ ํ„ฐ๋„์„ ์„ค์ •
  • TRACE: ๋Œ€์ƒ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ ๋ฉ”์‹œ์ง€ ๋ฃจํ”„๋ฐฑ ํ…Œ์ŠคํŠธ๋ฅผ ์ˆ˜ํ–‰

HTTP ๋ฉ”์„œ๋“œ์˜ ์†์„ฑ

  • ์•ˆ์ „(Safe Methods)
  • ๋ฉฑ๋“ฑ(Idenpotent Methods)
  • ์บ์‹œ๊ฐ€๋Šฅ(Cacheable Methods)

๊ฐ ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฐ€์ง€๋Š” ์†์„ฑ๋“ค

https://ko.wikipedia.org/wiki/HTTP

์•ˆ์ „(Safe)

  • ํ˜ธ์ถœํ•ด๋„ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋ฉฑ๋“ฑ(Idenpotent)

  • f(f(x)) = f(x)
  • ํ•œ ๋ฒˆ ํ˜ธ์ถœํ•˜๋˜ ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœํ•˜๋˜ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์•„์•ผํ•จ.
  • ๊ฐ™์€ ์š”์ฒญ์— ๋Œ€ํ•ด์„œ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๋ฉฑ๋“ฑ ๋ฉ”์„œ๋“œ
    • GET: ์—ฌ๋Ÿฌ๋ฒˆ ์กฐํšŒํ•ด๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์กฐํšŒ๋œ๋‹ค.
    • PUT: ๊ฒฐ๊ณผ๋ฅผ ๋Œ€์ฒด. ๋”ฐ๋ผ์„œ ๊ฐ™์€ ์š”์ฒญ์„ ์—ฌ๋Ÿฌ๋ฒˆ ํ•ด๋„ ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Œ
    • DELETE: ๊ฒฐ๊ณผ๋ฅผ ์‚ญ์ œ. ๊ฐ™์€ ์š”์ฒญ์„ ์—ฌ๋Ÿฌ๋ฒˆ ํ•ด๋„ ์‚ญ์ œ๋œ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Œ
    • POST: ๋ฉฑ๋“ฑ์ด ์•„๋‹˜. ๋‘ ๋ฒˆ ํ˜ธ์ถœํ•˜๋ฉด ๊ฐ™์€ ๊ฒฐ์ œ๊ฐ€ ์ค‘๋ณตํ•ด์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ
  • GET์— ๋Œ€ํ•ด์„œ ์ค‘๊ฐ„์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ฉฑ๋“ฑ์ด ์•„๋‹ˆ์ง€ ์•Š๋‚˜? ๋ผ๋Š” ์˜๋ฌธ์ ์ด ์ƒ๊ธฐ์ง€๋งŒ
  • ๋ฉฑ๋“ฑ์€ ์™ธ๋ถ€ ์š”์ธ์— ์˜ํ•ด์„œ ์ค‘๊ฐ„์— ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ๊นŒ์ง€ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ๋”ฐ๋ผ์„œ GET์„ ๋ฉฑ๋“ฑ์ด๋ผ๊ณ  ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์บ์‹œ ๊ฐ€๋Šฅ(Cacheable)

  • ์‘๋‹ต ๊ฒฐ๊ณผ ๋ฆฌ์†Œ์Šค๋ฅผ ์บ์‹œํ•ด์„œ ์‚ฌ์šฉํ•ด๋„ ๋˜๋Š”๊ฐ€?
  • GET, HEAD, POST, PATCH ์บ์‹œ๊ฐ€๋Šฅ
  • ์‹ค์ œ๋กœ๋Š” GET, HEAD ์ •๋„๋งŒ ์บ์‹œ๋กœ ์‚ฌ์šฉํ•จ
    • POST, PATCH๋Š” ๋ณธ๋ฌธ ๋‚ด์šฉ๊นŒ์ง€ ์บ์‹œ ํ‚ค๋กœ ๊ณ ๋ คํ•ด์•ผํ•˜๋Š”๋ฐ ๊ตฌํ˜„์ด ์‰ฝ์ง€ ์•Š์Œ.
๋ฐ˜์‘ํ˜•