REST, REST API, RESTful API

kirin.logยท2021๋…„ 4์›” 5์ผ
1
post-custom-banner

๐Ÿš— REST

๐Ÿช REST์˜ ์ •์˜

  • Representational State Transfer : ๋Œ€ํ‘œ์ ์ธ ์ƒํƒœ ์ „๋‹ฌ
  • REST๋ž€, ์›น์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์ž์›(์ด๋ฏธ์ง€, ๋™์˜์ƒ, DB์ž์› ๋“ฑ)์— ๊ณ ์œ ํ•œ URL์„ ๋ถ€์—ฌ ํ•ด ํ™œ์šฉํ•˜๋Š” ๊ฒƒ.
    • ์ฆ‰, ์ž์›์„ ์ •์˜ํ•˜๊ณ  ์ž์›์— ๋Œ€ํ•œ ์ฃผ์†Œ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋ก ์„ ์˜๋ฏธํ•œ๋‹ค.
    • ์ผ์ข…์˜ Coding Convention
    • ์ฆ‰, ์ž์›(resource)์˜ ํ‘œํ˜„(representation) ์— ์˜ํ•œ ์ƒํƒœ ์ „๋‹ฌ)

โ“ ์ž์›(resource)์˜ ํ‘œํ˜„(representation)
์ž์›: ํ•ด๋‹น ์†Œํ”„ํŠธ์›จ์–ด๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ
Ex) ๋ฌธ์„œ, ๊ทธ๋ฆผ, ๋ฐ์ดํ„ฐ, ํ•ด๋‹น ์†Œํ”„ํŠธ์›จ์–ด ์ž์ฒด ๋“ฑ

์ž์›์˜ ํ‘œํ˜„: ๊ทธ ์ž์›์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ด๋ฆ„
Ex) DB์˜ ํ•™์ƒ ์ •๋ณด๊ฐ€ ์ž์›์ผ ๋•Œ, โ€˜studentsโ€™๋ฅผ ์ž์›์˜ ํ‘œํ˜„์œผ๋กœ ์ •ํ•œ๋‹ค.

์ƒํƒœ(์ •๋ณด) ์ „๋‹ฌ:

  • ๋ฐ์ดํ„ฐ๊ฐ€ ์š”์ฒญ๋˜์–ด์ง€๋Š” ์‹œ์ ์—์„œ ์ž์›์˜ ์ƒํƒœ(์ •๋ณด)๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
  • JSON ํ˜น์€ XML๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค.
  • ์›”๋“œ ์™€์ด๋“œ ์›น(www)๊ณผ ๊ฐ™์€ ๋ถ„์‚ฐ ํ•˜์ดํผ๋ฏธ๋””์–ด ์‹œ์Šคํ…œ์„ ์œ„ํ•œ ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ ์•„ํ‚คํ…์ฒ˜์˜ ํ•œ ํ˜•์‹

REST๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์›น์˜ ๊ธฐ์กด ๊ธฐ์ˆ ๊ณผ HTTP ํ”„๋กœํ† ์ฝœ์„ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์›น์˜ ์žฅ์ ์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•„ํ‚คํ…์ฒ˜ ์Šคํƒ€์ผ์ด๋‹ค.


REST๋Š” ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ Client์™€ Server ์‚ฌ์ด์˜ ํ†ต์‹  ๋ฐฉ์‹ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

โ— ๋”ฐ๋ผ์„œ RESTful API๋ž€, REST์˜ ํŠน์ง•์„ ์ง€ํ‚ค๋ฉด์„œ API๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
โ— RESTful API๋ž€, ๊ฒฐ๊ตญ HTTP ํ”„๋กœํ† ์ฝœ ์žฅ์ ์„ ์‚ด๋ฆด ์ˆ˜ ์žˆ๋Š” ๋„คํŠธ์›Œํฌ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋œปํ•œ๋‹ค.


๐Ÿช REST๊ฐ€ ํ•„์š”ํ•œ ์ด์œ (๋“ฑ์žฅ๋ฐฐ๊ฒฝ)

  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ถ„๋ฆฌ ๋ฐ ํ†ตํ•ฉ
    ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์–ด๋–ป๊ฒŒ ๋ถ„๋ฆฌํ•˜๊ณ  ํ†ตํ•ฉ ํ•˜๋Š๋ƒ๊ฐ€ ์ฃผ์š” ์ด์Šˆ๊ฐ€ ๋˜์—ˆ๊ณ , ์ด์— REST๊ฐ€ ์ด์Šˆ๊ฐ€ ๋˜์—ˆ๋‹ค.

  • ๋‹ค์–‘ํ•œ ํด๋ผ์ด์–ธํŠธ์˜ ๋“ฑ์žฅ
    ๋ชจ๋ฐ”์ผ๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด์„œ backend ํ•˜๋‚˜๋กœ ๋‹ค์–‘ํ•œ ๋””๋ฐ”์ด์Šค๋ฅผ ๋Œ€์‘ํ•˜๊ธฐ ์œ„ํ•ด REST์˜ ํ•„์š”์„ฑ์ด ์ฆ๋Œ€ํ•˜์˜€๋‹ค.

์ด๋Ÿฌํ•œ ๋ฉ€ํ‹ฐ ํ”Œ๋žซํผ์— ๋Œ€ํ•œ ์ง€์›์„ ์œ„ํ•ด ์„œ๋น„์Šค ์ž์›์— ๋Œ€ํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ธ์šฐ๊ณ  ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ์ƒ‰ํ•œ ๊ฒฐ๊ณผ, REST์— ๊ด€์‹ฌ์„ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ๋‹ค.


๐Ÿช REST์˜ ๊ตฌ์ฒด์ ์ธ ๊ฐœ๋…

HTTP URI(Uniform Resource Identifier)๋ฅผ ํ†ตํ•ด ์ž์›(Resource)์„ ๋ช…์‹œํ•˜๊ณ , HTTP Method(POST, GET, PUT, DELETE)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ž์›์— ๋Œ€ํ•œ CRUD Operation์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

์ฆ‰, REST๋Š” ์ž์› ๊ธฐ๋ฐ˜์˜ ๊ตฌ์กฐ(ROA, Resource Oriented Architecture) ์„ค๊ณ„์˜ ์ค‘์‹ฌ์— Resource๊ฐ€ ์žˆ๊ณ ,
HTTP Method๋ฅผ ํ†ตํ•ด Resource๋ฅผ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์„ค๊ณ„๋œ ์•„ํ‚คํ…์ณ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

์›น ์‚ฌ์ดํŠธ์˜ ์ด๋ฏธ์ง€, ํ…์ŠคํŠธ, DB ๋‚ด์šฉ ๋“ฑ์˜ ๋ชจ๋“  ์ž์›์— ๊ณ ์œ ํ•œ ID์ธ HTTP URI๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค.

โ“ URI (Uniform Resource Indenfier)
์›น์„œ๋ฒ„์˜ ๋ฆฌ์†Œ์Šค๋Š” ๊ฐ์ž ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํด๋ผ์ด์–ธํŠธ๋Š” ์ด๋Ÿฌํ•œ ์ด๋ฆ„์„ ํ†ตํ•ด ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
์ด๋•Œ ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค์˜ ์ด๋ฆ„์„ ํ†ตํ•ฉ ์ž์› ์‹๋ณ„์ž ๋˜๋Š” URI ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด, ์นด์นด์˜ค ์‚ฌ์ดํŠธ๋ฅผ ์ ‘์†ํ•˜์—ฌ news๋ผ๋Š” ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.
๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•˜๋ฉด https://www.kakaocorp.com/kakao/introduce/news ์™€ ๊ฐ™์ด URI ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

  • https:// ๐Ÿ‘‰ HTTP ํ”„๋กœ์ฝœ์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋œป (๋’ค์˜ s ๋Š” ๊ธฐ์กด ํ”„๋กœ์ฝœ์—์„œ ๋ณด์•ˆ์ด ๊ฐ•ํ™”๋œ ๋ฒ„์ „)
  • www.kakaocorp.com ๐Ÿ‘‰ ์นด์นด์˜ค ์‚ฌ์ดํŠธ์˜ ์ฃผ์†Œ
  • /kakao/introduce/vision ๐Ÿ‘‰ ๋ฆฌ์†Œ์Šค ์œ„์น˜

โ“ CRUD Operation
Create : ์ƒ์„ฑ(POST)
Read : ์กฐํšŒ(GET)
Update : ์ˆ˜์ •(PUT)
Delete : ์‚ญ์ œ(DELETE)
HEAD: header ์ •๋ณด ์กฐํšŒ(HEAD)


๐Ÿช REST์˜ ์žฅ๋‹จ์ 

  • ์žฅ์ 

    • HTTP ํ”„๋กœํ† ์ฝœ์˜ ์ธํ”„๋ผ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ REST API ์‚ฌ์šฉ์„ ์œ„ํ•œ ๋ณ„๋„์˜ ์ธํ”„๋ผ๋ฅผ ๊ตฌ์ถœํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

    • HTTP ํ‘œ์ค€ ํ”„๋กœํ† ์ฝœ์— ๋”ฐ๋ฅด๋Š” ๋ชจ๋“  ํ”Œ๋žซํผ์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

    • Hypermedia API์˜ ๊ธฐ๋ณธ์„ ์ถฉ์‹คํžˆ ์ง€ํ‚ค๋ฉด์„œ ๋ฒ”์šฉ์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค.

    • REST API ๋ฉ”์‹œ์ง€๊ฐ€ ์˜๋„ํ•˜๋Š” ๋ฐ”๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ ์˜๋„ํ•˜๋Š” ๋ฐ”(๋ชฉ์ )๋ฅผ ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

      ex) ์š”์ฒญ ๋‚ด์šฉ์„ ๋ณด๊ณ  ์‰ฝ๊ฒŒ ํ•ด์„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
      https://api.trueshort.com/stock/003890 ์˜ ์ฃผ์†Œ๋งŒ ๋ด๋„,
      https://api.trueshort.com ๋ผ๋Š” API์—์„œ A์ „์ž ์ฃผ์‹์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ HTTP ์š”์ฒญ์„ ํ†ตํ•ด ๋ฐ›์•„์˜จ๋‹ค" ๋ผ๊ณ  ํ•ด์„์ด ์‰ฝ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

    • ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์„œ๋น„์Šค ๋””์ž์ธ์—์„œ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ์ตœ์†Œํ™”ํ•œ๋‹ค.

    • ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ์˜ ์—ญํ• ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•œ๋‹ค.

  • ๋‹จ์ 

    • ํ‘œ์ค€์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ 4๊ฐ€์ง€ ๋ฐ–์— ์—†๋‹ค. (HTTP Method ํ˜•ํƒœ๊ฐ€ ์ œํ•œ์ )
    • ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ํ…Œ์ŠคํŠธํ•  ์ผ์ด ๋งŽ์€ ์„œ๋น„์Šค๋ผ๋ฉด ์‰ฝ๊ฒŒ ๊ณ ์น  ์ˆ˜ ์žˆ๋Š” URL๋ณด๋‹ค Header ๊ฐ’์ด ์™ ์ง€ ๋” ์–ด๋ ต๊ฒŒ ๋Š๊ปด์ง„๋‹ค.
    • ๊ตฌํ˜• ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์•„์ง ์ œ๋Œ€๋กœ ์ง€์›ํ•ด์ฃผ์ง€ ๋ชปํ•˜๋Š” ๋ถ€๋ถ„์ด ์กด์žฌํ•œ๋‹ค. (PUT, DELETE๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋Š” ์  / pushState๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ์ )

๐Ÿช REST ๊ตฌ์„ฑ ์š”์†Œ

  • ์ž์›(Resource): URI
    • ๋ชจ๋“  ์ž์›์— ๊ณ ์œ ํ•œ ID๊ฐ€ ์กด์žฌํ•˜๊ณ , ์ด ์ž์›์€ Server์— ์กด์žฌํ•œ๋‹ค.
    • ์ž์›์„ ๊ตฌ๋ณ„ํ•˜๋Š” ID๋Š” โ€˜/groups/:group_idโ€™์™€ ๊ฐ™์€ HTTP URI ๋‹ค.
    • Client๋Š” URI๋ฅผ ์ด์šฉํ•ด์„œ ์ž์›์„ ์ง€์ •ํ•˜๊ณ  ํ•ด๋‹น ์ž์›์˜ ์ƒํƒœ(์ •๋ณด)์— ๋Œ€ํ•œ ์กฐ์ž‘์„ Server์— ์š”์ฒญํ•œ๋‹ค.
  • ํ–‰์œ„(Verb): HTTP Method
    • HTTP ํ”„๋กœํ† ์ฝœ์˜ Method๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (HTTP ํ”„๋กœํ† ์ฝœ์€ GET, POST, PUT, DELETE ์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.)
  • ํ‘œํ˜„(Representation of Resource)
    • Client๊ฐ€ ์ž์›์˜ ์ƒํƒœ(์ •๋ณด)์— ๋Œ€ํ•œ ์กฐ์ž‘์„ ์š”์ฒญํ•˜๋ฉด Server๋Š” ์ด์— ์ ์ ˆํ•œ ์‘๋‹ต(Representation)์„ ๋ณด๋‚ธ๋‹ค.
    • REST์—์„œ ํ•˜๋‚˜์˜ ์ž์›์€ JSON, XML, TEXT, RSS ๋“ฑ ์—ฌ๋Ÿฌ ํ˜•ํƒœ์˜ Representation์œผ๋กœ ๋‚˜ํƒ€๋‚ด์–ด ์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • JSON ํ˜น์€ XML๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค.

๐Ÿš– REST์˜ ๊ตฌ์„ฑ

  • ์ž์› (Resouce) - URI
  • ํ–‰์œ„ (Verb) - HTTP Method
  • ํ‘œํ˜„ (Representations)

EX)
/users GET
/users/{id} GET
/users PUT


์˜ˆ์‹œ) REST ๋ฐฉ์‹์ด ์ ์šฉ๋œ RESTful API

  • A์ฃผ์‹ ์ •๋ณด๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•œ HTTP ์š”์ฒญ
    ๐Ÿ‘‰ HTTP GET https://api.trueshort.com/stock/003890
    [HTTP] / [Method(GET): How to] / [what]
  • ์œ ์ €์˜ ๋ณด์œ  ์ฃผ์‹ ์ข…๋ชฉ๋“ค์„ DB์— ์ €์žฅํ•˜๋Š” HTTP ์š”์ฒญ
    ๐Ÿ‘‰ HTTP POST https://api.trueshort.com/users/portfolios
    [HTTP] / [Method(POST): How to] / [what]

๐Ÿš€ Client๊ฐ€ Server์—๊ฒŒ ์š”์ฒญํ•˜๋Š” ๋ฐฉ์‹.

  • HTTP Method (4๊ฐ€์ง€)
    • POST
    • GET
    • PUT(์ „์ฒด์ˆ˜์ • ์š”์ฒญ, ์ƒˆ๋กœ ๋ฎ์–ด์”Œ์šฐ๋Š” ๊ฒƒ),
    • DELETE(๋ฐ์ดํ„ฐ ์‚ญ์ œ ์š”์ฒญ),
    • PATCH(์ผ๋ถ€ ์ˆ˜์ • ์š”์ฒญ)
  • 4๊ฐ€์ง€ ์—ฐ์‚ฐ = INSERT, SELECT, UPDATE, DELETE
  • Payload = HTTP request์—์„œ server๋กœ ๋ณด๋‚ด๋Š” ๋ฐ์ดํ„ฐ(body)
  • URL : http://x.x.x.x:00000/path1/filename.txt
  • URI : http://x.x.x.x:00000/products/smartphone

โ— URI (Uniform Resource Identifier)
: ํ•ด๋‹น ์‚ฌ์ดํŠธ์˜ ํŠน์ • ์ž์›์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์œ ์ผํ•œ ์ฃผ์†Œ


๐Ÿช REST ํŠน์ง•

  • Uniform (์œ ๋‹ˆํผ ์ธํ„ฐํŽ˜์ด์Šค)

    • HTTP ํ‘œ์ค€์—๋งŒ ๋”ฐ๋ฅธ๋‹ค๋ฉด, ์•ˆ๋“œ๋กœ์ด๋“œ/IOS ํ”Œ๋žซํผ์ด๋“ , ํŠน์ • ์–ธ์–ด๋‚˜ ๊ธฐ์ˆ ์— ์ข…์†๋˜์ง€ ์•Š๊ณ  ๋ชจ๋“  ํ”Œ๋žซํผ์— ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, URI๋กœ ์ง€์ •ํ•œ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์กฐ์ž‘์ด ๊ฐ€๋Šฅํ•œ ์•„ํ‚คํ…์ฒ˜ ์Šคํƒ€์ผ์„ ์˜๋ฏธํ•œ๋‹ค.
  • Self-descriptiveness (์ž์ฒด ํ‘œํ˜„ ๊ตฌ์กฐ) : ๋™์‚ฌ(Method) + ๋ช…์‚ฌ(URI) ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์–ด ์–ด๋–ค ๋ฉ”์„œ๋“œ์— ๋ฌด์Šจ ํ–‰์œ„๋ฅผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฉ”์‹œ์ง€ ํฌ๋งท ์—ญ์‹œ JSON์„ ์ด์šฉํ•ด์„œ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ด๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ๋กœ, REST API ๋ฉ”์‹œ์ง€๋งŒ ๋ณด๊ณ ๋„ ์ด๋ฅผ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Server-Client(์„œ๋ฒ„-ํด๋ผ์ด์–ธํŠธ ๊ตฌ์กฐ)

    • ์ž์›์ด ์žˆ๋Š” ์ชฝ์ด Server, ์ž์›์„ ์š”์ฒญํ•˜๋Š” ์ชฝ์ด Client๊ฐ€ ๋œ๋‹ค.

    • REST Server: API๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ ๋ฐ ์ €์žฅ์„ ์ฑ…์ž„์ง„๋‹ค.

    • Client: ์‚ฌ์šฉ์ž ์ธ์ฆ์ด๋‚˜ context(์„ธ์…˜, ๋กœ๊ทธ์ธ ์ •๋ณด) ๋“ฑ์„ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๊ณ  ์ฑ…์ž„์ง„๋‹ค.
      ์„œ๋กœ ๊ฐ„ ์˜์กด์„ฑ์ด ์ค„์–ด๋“ ๋‹ค.

      ๐Ÿ‘‰ REST ์„œ๋ฒ„๋Š” API ์ œ๊ณต, ํด๋ผ์ด์–ธํŠธ๋Š” ์‚ฌ์šฉ์ž ์ธ์ฆ์ด๋‚˜ ์ปจํ…์ŠคํŠธ(์„ธ์…˜, ๋กœ๊ทธ์ธ ์ •๋ณด ๋“ฑ)์„ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๋Š” ๊ตฌ์กฐ๋กœ ๊ฐ๊ฐ์˜ ์—ญํ• ์ด ํ™•์‹คํžˆ ๊ตฌ๋ถ„๋˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์—์„œ ๊ฐœ๋ฐœํ•ด์•ผ ํ•  ๋‚ด์šฉ์ด ๋ช…ํ™•ํ•ด์ง€๊ณ  ์„œ๋กœ๊ฐ„ ์˜์กด์„ฑ์ด ์ค„์–ด๋“ค๊ฒŒ ๋œ๋‹ค.

  • Stateless(๋ฌด์ƒํƒœ)

    • HTTP ํ”„๋กœํ† ์ฝœ์€ Stateless Protocol์ด๋ฏ€๋กœ REST ์—ญ์‹œ ๋ฌด์ƒํƒœ์„ฑ์„ ๊ฐ–๋Š”๋‹ค.
    • Client์˜ context๋ฅผ Server์— ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.
      ์ฆ‰, ์„ธ์…˜๊ณผ ์ฟ ํ‚ค์™€ ๊ฐ™์€ context ์ •๋ณด๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋˜๋ฏ€๋กœ ๊ตฌํ˜„์ด ๋‹จ์ˆœํ•ด์ง„๋‹ค. (์ €์žฅ์†Œ์— ์ƒํƒœ์ •๋ณด๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ , API ์„œ๋ฒ„๋Š” ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ๋งŒ์„ ๋‹จ์ˆœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.)
    • Server๋Š” ๊ฐ๊ฐ์˜ ์š”์ฒญ์„ ์™„์ „ํžˆ ๋ณ„๊ฐœ์˜ ๊ฒƒ์œผ๋กœ ์ธ์‹ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•œ๋‹ค.
    • ๊ฐ API ์„œ๋ฒ„๋Š” Client์˜ ์š”์ฒญ๋งŒ์„ ๋‹จ์ˆœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๐Ÿ‘‰ ์ฆ‰, ์ด์ „ ์š”์ฒญ์ด ๋‹ค์Œ ์š”์ฒญ์˜ ์ฒ˜๋ฆฌ์— ์—ฐ๊ด€๋˜์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค.(์ผํšŒ์„ฑ)
      โ— ๋ฌผ๋ก  ์ด์ „ ์š”์ฒญ์ด DB๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ DB์— ์˜ํ•ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์€ ํ—ˆ์šฉํ•œ๋‹ค.
    • Server์˜ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์— ์ผ๊ด€์„ฑ์„ ๋ถ€์—ฌํ•˜๊ณ  ๋ถ€๋‹ด์ด ์ค„์–ด๋“ค๋ฉฐ, ์„œ๋น„์Šค์˜ ์ž์œ ๋„๊ฐ€ ๋†’์•„์ง„๋‹ค.
  • Cacheable(์บ์‹œ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ)

    • ์›น ํ‘œ์ค€ HTTP ํ”„๋กœํ† ์ฝœ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์›น์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์กด์˜ ์ธํ”„๋ผ๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
      ์ฆ‰, HTTP๊ฐ€ ๊ฐ€์ง„ ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ํŠน์ง• ์ค‘ ํ•˜๋‚˜์ธ ์บ์‹ฑ ๊ธฐ๋Šฅ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • HTTP ํ”„๋กœํ† ์ฝœ ํ‘œ์ค€์—์„œ ์‚ฌ์šฉํ•˜๋Š” Last-Modified ํƒœ๊ทธ๋‚˜ E-Tag๋ฅผ ์ด์šฉํ•˜๋ฉด ์บ์‹ฑ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ๋Œ€๋Ÿ‰์˜ ์š”์ฒญ์„ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์บ์‹œ๊ฐ€ ์š”๊ตฌ๋œ๋‹ค.
    • ์บ์‹œ ์‚ฌ์šฉ์„ ํ†ตํ•ด ์‘๋‹ต์‹œ๊ฐ„์ด ๋นจ๋ผ์ง€๊ณ  REST Server ํŠธ๋žœ์žญ์…˜์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ์‘๋‹ต์‹œ๊ฐ„, ์„ฑ๋Šฅ, ์„œ๋ฒ„์˜ ์ž์› ์ด์šฉ๋ฅ ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿš— REST API

  • API(Application Programming Interface)๋ž€
    ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์˜ ์ง‘ํ•ฉ์„ ์ œ๊ณตํ•˜์—ฌ ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋žจ๊ฐ„ ์ƒํ˜ธ์ž‘์šฉ์„ ์ด‰์ง„ํ•˜๋ฉฐ, ์„œ๋กœ ์ •๋ณด๋ฅผ ๊ตํ™˜๊ฐ€๋Šฅ ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ
  • REST API์˜ ์ •์˜
    • REST ๊ธฐ๋ฐ˜์œผ๋กœ ์„œ๋น„์Šค API๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ
    • ์ตœ๊ทผ OpenAPI(๋ˆ„๊ตฌ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณต๊ฐœ๋œ API: ๊ตฌ๊ธ€ ๋งต, ๊ณต๊ณต ๋ฐ์ดํ„ฐ ๋“ฑ), ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค(ํ•˜๋‚˜์˜ ํฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ ์ชผ๊ฐœ์–ด ๋ณ€๊ฒฝ๊ณผ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“  ์•„ํ‚คํ…์ฒ˜) ๋“ฑ์„ ์ œ๊ณตํ•˜๋Š” ์—…์ฒด ๋Œ€๋ถ€๋ถ„์€ REST API๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • REST API์˜ ํŠน์ง•
    • ์‚ฌ๋‚ด ์‹œ์Šคํ…œ๋“ค๋„ REST ๊ธฐ๋ฐ˜์œผ๋กœ ์‹œ์Šคํ…œ์„ ๋ถ„์‚ฐํ•ด ํ™•์žฅ์„ฑ๊ณผ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ ์œ ์ง€๋ณด์ˆ˜ ๋ฐ ์šด์šฉ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • REST๋Š” HTTP ํ‘œ์ค€์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฏ€๋กœ, HTTP๋ฅผ ์ง€์›ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์–ธ์–ด๋กœ ํด๋ผ์ด์–ธํŠธ, ์„œ๋ฒ„๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
      ์ฆ‰, REST API๋ฅผ ์ œ์ž‘ํ•˜๋ฉด ๋ธํŒŒ์ด ํด๋ผ์ด์–ธํŠธ ๋ฟ ์•„๋‹ˆ๋ผ, ์ž๋ฐ”, C#, ์›น ๋“ฑ์„ ์ด์šฉํ•ด ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿช REST API์˜ ํŠน์ง•

  • REST ๊ธฐ๋ฐ˜์œผ๋กœ ์‹œ์Šคํ…œ์„ ๋ถ„์‚ฐํ•ด ํ™•์žฅ์„ฑ๊ณผ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ ์œ ์ง€๋ณด์ˆ˜ ๋ฐ ์šด์šฉ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • REST๋Š” HTTP ํ‘œ์ค€์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฏ€๋กœ, HTTP๋ฅผ ์ง€์›ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์–ธ์–ด๋กœ ํด๋ผ์ด์–ธํŠธ, ์„œ๋ฒ„๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ก REST API ์„ค๊ณ„ ๊ธฐ๋ณธ ๊ทœ์น™

์ฐธ๊ณ  ๋ฆฌ์†Œ์Šค ์›ํ˜•

๋„ํ๋จผํŠธ(document) : ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ ˆ์ฝ”๋“œ์™€ ์œ ์‚ฌํ•œ ๊ฐœ๋…
์ปฌ๋ ‰์…˜(collection) : ์„œ๋ฒ„์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋ผ๋Š” ๋ฆฌ์†Œ์Šค
์Šคํ† ์–ด(store) : ํด๋ผ์ด์–ธํŠธ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋ฆฌ์†Œ์Šค ์ €์žฅ์†Œ


1) URI๋Š” ์ •๋ณด์˜ ์ž์›(resource)์„ ํ‘œํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

  • resource๋Š” ๋™์‚ฌ๋ณด๋‹ค๋Š” ๋ช…์‚ฌ๋ฅผ, ๋Œ€๋ฌธ์ž๋ณด๋‹ค๋Š” ์†Œ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • resource์˜ ๋„ํ๋จผํŠธ ์ด๋ฆ„์œผ๋กœ๋Š” ๋‹จ์ˆ˜ ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • resource์˜ ์ปฌ๋ ‰์…˜ ์ด๋ฆ„์œผ๋กœ๋Š” ๋ณต์ˆ˜ ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • resource์˜ ์Šคํ† ์–ด ์ด๋ฆ„์œผ๋กœ๋Š” ๋ณต์ˆ˜ ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

Ex) GET /Member/1 ๐Ÿ‘‰ GET /members/1


2) ์ž์›์— ๋Œ€ํ•œ ํ–‰์œ„๋Š” HTTP Method(GET, PUT, POST, DELETE ๋“ฑ)๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

  • URI์— HTTP Method๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ๋œ๋‹ค.
    Ex) GET /members/delete/1 ๐Ÿ‘‰ DELETE /members/1

  • URI์— ํ–‰์œ„์— ๋Œ€ํ•œ ๋™์‚ฌ ํ‘œํ˜„์ด ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ๋œ๋‹ค.(์ฆ‰, CRUD ๊ธฐ๋Šฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์€ URI์— ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.)
    Ex) GET /members/show/1 ๐Ÿ‘‰ GET /members/1
    Ex) GET /members/insert/2 ๐Ÿ‘‰ POST /members/2

  • ๊ฒฝ๋กœ ๋ถ€๋ถ„ ์ค‘ ๋ณ€ํ•˜๋Š” ๋ถ€๋ถ„์€ ์œ ์ผํ•œ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค.(์ฆ‰, :id๋Š” ํ•˜๋‚˜์˜ ํŠน์ • resource๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ณ ์œ ๊ฐ’์ด๋‹ค.)
    Ex) student๋ฅผ ์ƒ์„ฑํ•˜๋Š” route ๐Ÿ‘‰ POST /students
    Ex) id=12์ธ student๋ฅผ ์‚ญ์ œํ•˜๋Š” route ๐Ÿ‘‰ DELETE /students/12


  • REST API ์„ค๊ณ„ ๊ทœ์น™

    • ์Šฌ๋ž˜์‹œ ๊ตฌ๋ถ„์ž(/)๋Š” ๊ณ„์ธต ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.
      Ex) http://restapi.example.com/houses/apartments

    • URI์— ํฌํ•จ๋˜๋Š” ๋ชจ๋“  ๊ธ€์ž๋Š” ๋ฆฌ์†Œ์Šค์˜ ์œ ์ผํ•œ ์‹๋ณ„์ž๋กœ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•˜๋ฉฐ URI๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์€ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ์—ญ์œผ๋กœ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋‹ค๋ฅด๋ฉด URI๋„ ๋‹ฌ๋ผ์ ธ์•ผ ํ•œ๋‹ค.

    • REST API๋Š” ๋ถ„๋ช…ํ•œ URI๋ฅผ ๋งŒ๋“ค์–ด ํ†ต์‹ ์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผ๋™์„ ์ฃผ์ง€ ์•Š๋„๋ก URI ๊ฒฝ๋กœ์˜ ๋งˆ์ง€๋ง‰์—๋Š” ์Šฌ๋ž˜์‹œ(/)๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
      Ex) http://restapi.example.com/houses/apartments/ (X)

    • ํ•˜์ดํ”ˆ(- )์€ URI ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š”๋ฐ ์‚ฌ์šฉ ๐Ÿ‘‰ ๋ถˆ๊ฐ€ํ”ผํ•˜๊ฒŒ ๊ธด URI๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํ•˜์ดํ”ˆ์„ ์‚ฌ์šฉํ•ด ๊ฐ€๋…์„ฑ์„ ๋†’์ธ๋‹ค.

    • ๋ฐ‘์ค„(_ )์€ URI์— ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๐Ÿ‘‰ ๋ฐ‘์ค„์€ ๋ณด๊ธฐ ์–ด๋ ต๊ฑฐ๋‚˜ ๋ฐ‘์ค„ ๋•Œ๋ฌธ์— ๋ฌธ์ž๊ฐ€ ๊ฐ€๋ ค์ง€๊ธฐ๋„ ํ•˜๋ฏ€๋กœ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ๋ฐ‘์ค„์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

    • URI ๊ฒฝ๋กœ์—๋Š” ์†Œ๋ฌธ์ž๊ฐ€ ์ ํ•ฉํ•˜๋‹ค. ๐Ÿ‘‰ URI ๊ฒฝ๋กœ์— ๋Œ€๋ฌธ์ž ์‚ฌ์šฉ์€ ํ”ผํ•˜๋„๋ก ํ•œ๋‹ค.
      (RFC 3986(URI ๋ฌธ๋ฒ• ํ˜•์‹)์€ URI ์Šคํ‚ค๋งˆ์™€ ํ˜ธ์ŠคํŠธ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜๋„๋ก ๊ทœ์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ)

    • ํŒŒ์ผํ™•์žฅ์ž๋Š” URI์— ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ๐Ÿ‘‰ REST API์—์„œ๋Š” ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ๋‚ด์šฉ์˜ ํฌ๋งท์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ URI ์•ˆ์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.

    • Accept header๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
      Ex) http://restapi.example.com/members/soccer/345/photo.jpg (X)
      Ex) GET / members/soccer/345/photo HTTP/1.1 Host: restapi.example.com Accept: image/jpg (O)

    • ๋ฆฌ์†Œ์Šค ๊ฐ„์—๋Š” ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
      /๋ฆฌ์†Œ์Šค๋ช…/๋ฆฌ์†Œ์Šค ID/๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค๋ช…
      Ex) GET : /users/{userid}/devices (์ผ๋ฐ˜์ ์œผ๋กœ ์†Œ์œ  โ€˜hasโ€™์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ)


  • REST API ์„ค๊ณ„ ์˜ˆ์‹œ

์ฐธ๊ณ  ์‘๋‹ต์ƒํƒœ์ฝ”๋“œ

1xx : ์ „์†ก ํ”„๋กœํ† ์ฝœ ์ˆ˜์ค€์˜ ์ •๋ณด ๊ตํ™˜
2xx : ํด๋ผ์–ด์ธํŠธ ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋จโœจโœจโœจ
3xx : ํด๋ผ์ด์–ธํŠธ๋Š” ์š”์ฒญ์„ ์™„๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์ธ ํ–‰๋™์„ ์ทจํ•ด์•ผ ํ•จ
4xx : ํด๋ผ์ด์–ธํŠธ์˜ ์ž˜๋ชป๋œ ์š”์ฒญ
5xx : ์„œ๋ฒ„์ชฝ ์˜ค๋ฅ˜๋กœ ์ธํ•œ ์ƒํƒœ์ฝ”๋“œ
RESTful์˜ ๊ฐœ๋…

๐Ÿš— RESTful

  • RESTful์€ ์ผ๋ฐ˜์ ์œผ๋กœ REST๋ผ๋Š” ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์›น ์„œ๋น„์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์šฉ์–ด์ด๋‹ค.
  • โ€˜REST APIโ€™๋ฅผ ์ œ๊ณตํ•˜๋Š” ์›น ์„œ๋น„์Šค๋ฅผ โ€˜RESTfulโ€™ํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • RESTful์€ REST๋ฅผ REST๋‹ต๊ฒŒ ์“ฐ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ, ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๊ณต์‹์ ์œผ๋กœ ๋ฐœํ‘œํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.
    ์ฆ‰, REST ์›๋ฆฌ๋ฅผ ๋”ฐ๋ฅด๋Š” ์‹œ์Šคํ…œ์€ RESTful์ด๋ž€ ์šฉ์–ด๋กœ ์ง€์นญ๋œ๋‹ค.

๐Ÿช RESTful์˜ ๋ชฉ์ 

  • ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šด REST API๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ
  • RESTfulํ•œ API๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ทผ๋ณธ์ ์ธ ๋ชฉ์ ์ด ์„ฑ๋Šฅ ํ–ฅ์ƒ์— ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ผ๊ด€์ ์ธ ์ปจ๋ฒค์…˜์„ ํ†ตํ•œ API์˜ ์ดํ•ด๋„ ๋ฐ ํ˜ธํ™˜์„ฑ์„ ๋†’์ด๋Š” ๊ฒƒ์ด ์ฃผ ๋™๊ธฐ์ด๋‹ˆ, ์„ฑ๋Šฅ์ด ์ค‘์š”ํ•œ ์ƒํ™ฉ์—์„œ๋Š” ๊ตณ์ด RESTfulํ•œ API๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š”๋Š” ์—†๋‹ค.
  • RESTful ํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
    Ex1) CRUD ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ POST๋กœ๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” API
    Ex2) route์— resource, id ์™ธ์˜ ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ(/students/updateName)

๐Ÿš— RESTful API

  • ์ •์˜
    • API ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์•„ํ‚คํ…์ฒ˜ ์ค‘์— ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ํ˜•์‹(๊ทœ์น™, rule)
    • ์›น์ƒ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์—ฌ๋Ÿฌ ๋ฆฌ์†Œ์Šค๋ฅผ HTTP URI๋กœ ํ‘œํ˜„ํ•˜๊ณ , ๊ทธ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ํ–‰์œ„๋ฅผ HTTP Method๋กœ ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹.
    • ์ฆ‰, "HTTP URI๋กœ ์ •์˜๋œ ๋ฆฌ์†Œ์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ํ•œ๋‹ค(HTTP Method + Payload)"๋ฅผ ๊ตฌ์กฐ์ ์œผ๋กœ ํ‘œํ˜„.

๐Ÿš– RESTful API ์„ค๊ณ„ ๊ทœ์น™

1) URI ์ •๋ณด๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

  • resource๋Š” ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    (โ—๋™์‚ฌ๋Š” ์ ˆ๋Œ€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค ๐Ÿ‘‰ HTTP Method์—๋งŒ ๋™์‚ฌ ์‚ฌ์šฉ)
    ex) GET /user /1
    ex) GET /users /1

2) resource์— ๋Œ€ํ•œ ํ–‰์œ„(๋™์‚ฌ)๋ฅผ HTTP Method(GET, POST, PUT, DELETE)๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

  • URI์— HTTP Method๊ฐ€ ํฌํ•จ๋˜์„œ๋Š” ์•ˆ๋œ๋‹ค.
    ex) GET delete /user /1 (โŒ)
    ex) DELETE /user /1 (โญ•)

  • URI์— ๋™์‚ฌ๊ฐ€ ํฌํ•จ๋˜์„œ๋Š” ์•ˆ๋œ๋‹ค.
    ex) GET /user /show /1 (โŒ)
    ex) GET /user /1 (โญ•)

    ex) POST insert /user /2 (โŒ)
    ex) POST /user /2 (โญ•)

3) resource ์‚ฌ์ด์— ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ /(์Šฌ๋ž˜์‹œ)๋กœ ๋ถ™์—ฌ๋‚˜๊ฐ„๋‹ค (๊ณ„์ธต๊ด€๊ณ„)

  • /๋ฆฌ์†Œ์Šค /๊ณ ์œ id /๊ด€๊ณ„ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค
    ex) GET /user /{user_id} /profile
    ex) GET /user /1 /profile
    (user์˜ id๋Š” 1์ด๊ณ , ๊ทธ์˜ profile์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ)
    ex) GET /user /1 /profile/2
    (user์˜ id๋Š” 1์ด๊ณ , ๊ทธ์˜ profile ์ค‘ 2๋ฒˆ profile์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ)
    โ— URI ๋งˆ์ง€๋ง‰ ๋ฌธ์ž ๋’ค์—๋Š” /๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.

4) ํŒŒ์ผ์˜ ๊ฒฝ์šฐ, payload์˜ ํฌ๋งท์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ URI์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.

ex) GET user/1/profile-photo.jpg (โŒ)
ex) GET user/1/profile-photo (โญ•)


๐Ÿš– Status Code

200 OK / [SUCCESS]
๐Ÿ‘‰ GET(๋ฆฌ์†Œ์Šค๋ฐ˜ํ™˜), PUT & PATCH(์ƒํƒœ ๋ฉ”์‹œ์ง€ ๋˜๋Š” ๋ฆฌ์†Œ์Šค ๋ฐ˜ํ™˜)
201 Created / [SUCCESS]
๐Ÿ‘‰ POST(์ƒํƒœ๋ฉ”์‹œ์ง€ ๋˜๋Š” ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๋ฆฌ์†Œ์Šค ๋ฐ˜ํ™˜)
204 No Content / [SUCCESS]
๐Ÿ‘‰ DELETE(์„ฑ๊ณต์ ์œผ๋กœ ์‚ญ์ œ๋œ ์š”์ฒญ์˜ ์‘๋‹ต)
400 Bad Request / [FAILURE]
๐Ÿ‘‰ All(์š”์ฒญ์— ์ž˜๋ชป๋œ ๊ฐ’๋“ค์ด ํฌํ•จ๋จ - ์ „ํ™”๋ฒˆํ˜ธ์— ๊ธ€์ž๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ)
401 Unauthorized / [FAILURE]
๐Ÿ‘‰ ALL(์ธ์ฆ์„ ์š”์ฒญ ํ–ˆ์œผ๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ ์š”๊ฑด์„ ์ถฉ์กฑํ•˜์ง€ ๋ชปํ•จ - ๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ)
403 Forbidden / [FAILURE]
๐Ÿ‘‰ ALL(์‚ฌ์šฉ์ž๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์€ ์ฝ˜ํ…์ธ ๋กœ ์ ‘๊ทผ์„ ์‹œ๋„ํ•จ - ๋น„์šฉ์„ ์ง€๋ถˆํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ)
404 Not Found / [FAILURE]
๐Ÿ‘‰ ALL(๋ฆฌ์†Œ์Šค ์—†์Œ)
405 Method Not Allowed / [FAILURE]
๐Ÿ‘‰ ALL(ํ—ˆ๊ฐ€๋˜์ง€ ์•Š์€ HTTP Method๋กœ ์‹œ๋„๋จ)
500
๐Ÿ‘‰ ์„œ๋ฒ„์ธก ์˜ค๋ฅ˜

https://brainbackdoor.tistory.com/53

profile
boma91@gmail.com
post-custom-banner

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