Home > Network > 2024 > ๐ŸŒ[Network] RESTful API๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐ŸŒ[Network] RESTful API๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?
Network

๐ŸŒ[Network] RESTful API๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

  • RESTful API๋Š” REST(Representational State Transfer) ์•„ํ‚คํ…์ฒ˜ ์Šคํƒ€์ผ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์›น API๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • RESTful API๋Š” ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ํ†ต์‹ ์„ ํšจ์œจ์ ์ด๊ณ  ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์„ค๊ณ„ํ•˜๊ธฐ ์œ„ํ•ด HTTP ํ”„๋กœํ† ์ฝœ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • RESTful์€ ํŠน์ • ๊ธฐ์ˆ ์ด๋‚˜ ํ”„๋กœํ† ์ฝœ์„ ์ง€์นญํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, REST ์•„ํ‚คํ…์ฒ˜์˜ ์›์น™์„ ์ค€์ˆ˜ํ•˜๋Š” API๋ฅผ ์„ค๋ช…ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์šฉ์–ด์ž…๋‹ˆ๋‹ค.

1๏ธโƒฃ REST(Representational State Transfer)์˜ ๊ธฐ๋ณธ ๊ฐœ๋….

  • REST๋Š” Roy Fielding์ด 2000๋…„ ๋ฐ•์‚ฌ ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœํ•œ ๋ถ„์‚ฐ ์‹œ์Šคํ…œ์„ ์„ค๊ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์•„ํ‚คํ…์ฒ˜ ์Šคํƒ€์ผ์ž…๋‹ˆ๋‹ค.
    • RESTful API๋Š” ์ด ์›์น™์„ ๋”ฐ๋ฅด๋Š” API๋กœ, ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ ๊ตฌ์กฐ, ๋ฌด์ƒํƒœ์„ฑ, ์บ์‹ฑ ๊ฐ€๋Šฅ์„ฑ, ๊ณ„์ธตํ™”๋œ ์‹œ์Šคํ…œ๊ณผ ๊ฐ™์€ REST์˜ ์ฃผ์š” ์ œ์•ฝ์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

2๏ธโƒฃ REST์˜ ์ฃผ์š” ์›์น™ ๋ฐ RESTful API์˜ ํŠน์ง•.

1๏ธโƒฃ ์ž์›(Resource) ๊ธฐ๋ฐ˜.

  • REST์—์„œ๋Š” ๋ชจ๋“  ๊ฒƒ์„ ์ž์›์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž๋Š” ํ•˜๋‚˜์˜ ์ž์›, ๊ฒŒ์‹œ๋ฌผ์€ ๋˜ ๋‹ค๋ฅธ ํ•˜๋‚˜์˜ ์ž์›์œผ๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค.
  • ๊ฐ ์ž์›์€ ๊ณ ์œ ํ•œ URI(Uniform Resource Identifer)๋ฅผ ํ†ตํ•ด ์‹๋ณ„๋ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, https://api.example.com/user/1๋Š” id=1์ธ ์‚ฌ์šฉ์ž ์ž์›์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

๐Ÿ‘‰ URI ์„ค๊ณ„ ์˜ˆ์‹œ

  • GET /users -> ๋ชจ๋“  ์‚ฌ์šฉ์ž ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
  • GET /users/1 -> ํŠน์ • ์‚ฌ์šฉ์ž(id=1) ๊ฐ€์ ธ์˜ค๊ธฐ
  • POST /users/1 -> ์ƒˆ ์‚ฌ์šฉ์ž ์ƒ์„ฑ
  • PUT /users/1 -> ์‚ฌ์šฉ์ž ์ •๋ณด ์—…๋ฐ์ดํŠธ
  • DELETE /users/1 -> ์‚ฌ์šฉ์ž ์‚ญ์ œ

2๏ธโƒฃ HTTP ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ.

  • RESTful API๋Š” HTTP ๋ฉ”์„œ๋“œ๋ฅผ ์ž์›์˜ ์ž‘์—…๊ณผ ์—ฐ๊ด€์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ์ฃผ์š” ๋ฉ”์„œ๋“œ๋กœ๋Š” GET, POST, PUT, DELETE๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
      • GET : ์ž์›์„ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
      • POST : ์ž์›์„ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
      • PUT : ์ž์›์„ ์—…๋ฐ์ดํŠธํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
      • DELETE : ์ž์›์„ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ HTTP ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ์€ ์š”์ฒญ์˜ ๋ชฉ์ ์„ ๋ช…ํ™•ํžˆ ํ•˜๊ณ , ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

3๏ธโƒฃ ๋ฌด์ƒํƒœ์„ฑ(Stateless)

  • RESTful API๋Š” ๋ฌด์ƒํƒœ(Stateless)ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ์ฆ‰, ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
      • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ๊ฐ๊ฐ์˜ ์š”์ฒญ์€ ๋…๋ฆฝ์ ์ด์–ด์•ผ ํ•˜๋ฉฐ, ์š”์ฒญ์— ์˜์กดํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ๋Š” ๋งค ์š”์ฒญ๋งˆ๋‹ค ํ•„์š”ํ•œ ๋ชจ๋“  ์ •๋ณด๋ฅผ ์„œ๋ฒ„์— ๋ณด๋‚ด์•ผ ํ•˜๋ฉฐ, ์„œ๋ฒ„๋Š” ํ•ด๋‹น ์š”์ฒญ๋งŒ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

4๏ธโƒฃ ์บ์‹œ ๊ฐ€๋Šฅ(Cacheable)

  • RESTful API์—์„œ ์‘๋‹ต์€ ์บ์‹œ ๋  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ์„œ๋ฒ„๋Š” ์‘๋‹ต์— ๋Œ€ํ•œ ์บ์‹ฑ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ช…ํ™•ํžˆ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ž์›์˜ ๋ถˆํ•„์š”ํ•œ ์š”์ฒญ์„ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด, ์„œ๋ฒ„๋Š” HTTP ํ—ค๋”์— Cache-Control์ด๋‚˜ Expires์™€ ๊ฐ™์€ ์บ์‹œ ๊ด€๋ จ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜์—ฌ ์‘๋‹ต์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

5๏ธโƒฃ ๊ณ„์ธตํ™”๋œ ๊ตฌ์กฐ.

  • RESTful ์•„ํ‚คํ…์ฒ˜๋Š” ๊ณ„์ธตํ™”๋œ ์‹œ์Šคํ…œ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
    • ํด๋ผ์ด์–ธํŠธ๋Š” ์ค‘๊ฐ„ ์„œ๋ฒ„(ํ”„๋ก์‹œ, ๊ฒŒ์ดํŠธ์›จ์ด ๋“ฑ)๋ฅผ ํ†ต๊ณผํ•˜๋”๋ผ๋„, ์š”์ฒญ๊ณผ ์‘๋‹ต์ด ์ฒ˜๋ฆฌ๋˜๋Š” ๋ฐฉ์‹์— ๋Œ€ํ•ด ์‹ ๊ฒฝ ์“ธ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
    • ์„œ๋ฒ„๋Š” ์—ฌ๋Ÿฌ ๊ณ„์ธต์„ ํ†ตํ•ด ๋ณด์•ˆ, ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

6๏ธโƒฃ ์ผ๊ด€๋œ ์ธํ„ฐํŽ˜์ด์Šค.

  • RESTful API๋Š” ์ผ๊ด€๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋Š” ์ž์›์— ์ ‘๊ทผํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ๋ฐฉ์‹์ด ๋ช…ํ™•ํ•˜๊ณ  ์ผ๊ด€์ ์ด์–ด์•ผ ํ•˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๊ฐ ์ž์›์— ๋Œ€ํ•ด ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

7๏ธโƒฃ ํ‘œํ˜„(Representation) ์ „์†ก.

  • ์ž์› ์ž์ฒด๋Š” ์„œ๋ฒ„์— ์ €์žฅ๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋ฒ„์—์„œ ์ž์›์˜ ํ‘œํ˜„(Representation) ์„ ์ „์†ก๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ์ผ๋ฐ˜์ ์œผ๋กœ RESTful API๋Š” JSON์ด๋‚˜ XML ํ˜•์‹์œผ๋กœ ์ž์›์˜ ์ƒํƒœ๋ฅผ ํ‘œํ˜„ํ•˜๋ฉฐ, ์ด๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์˜ˆ์‹œ: ์„œ๋ฒ„๊ฐ€ ์‚ฌ์šฉ์ž ์ž์›์„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ JSON ํ˜•์‹์œผ๋กœ ์‘๋‹ตํ•  ๋•Œ.

{
    "id": 1,
    "name": "Kobe",
    "email": "kobe@example.com"
}

3๏ธโƒฃ RESTful API์˜ HTTP ๋ฉ”์„œ๋“œ์™€ ์ž์›์˜ ๊ด€๊ณ„.

1๏ธโƒฃ GET

  • ์ž์›์„ ์กฐํšŒํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: GET /users/1์€ id=1์ธ ์‚ฌ์šฉ์ž๋ฅผ ์กฐํšŒํ•˜๋Š” ์š”์ฒญ์ž…๋‹ˆ๋‹ค.

2๏ธโƒฃ POST

  • ์ƒˆ๋กœ์šด ์ž์›์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: POST /users๋Š” ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์š”์ฒญ์ž…๋‹ˆ๋‹ค.
  • ์š”์ฒญ ๋ณธ๋ฌธ์—๋Š” ์ƒ์„ฑํ•  ์ž์›์˜ ์ •๋ณด๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ PUT

  • ๊ธฐ์กด ์ž์›์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: PUT /users/1์€ id=1์ธ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์š”์ฒญ์ž…๋‹ˆ๋‹ค.

4๏ธโƒฃ DELETE

  • ์ž์›์„ ์‚ญ์ œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์˜ˆ: DELETE /users/1์€ id=1์ธ ์‚ฌ์šฉ์ž๋ฅผ ์‚ญ์ œํ•˜๋Š” ์š”์ฒญ์ž…๋‹ˆ๋‹ค.

4๏ธโƒฃ RESTful API์˜ ์˜ˆ

๐Ÿ‘‰ ์‚ฌ์šฉ์ž ์ž์›์„ ๊ด€๋ฆฌํ•˜๋Š” RESTful API ์˜ˆ์‹œ.

1๏ธโƒฃ GET ์š”์ฒญ (๋ชจ๋“  ์‚ฌ์šฉ์ž ์กฐํšŒ)

GET /users

์‘๋‹ต

[
    {"id": 1, "name": "Kobe", "email": "kobe@example.com"},
    {"id": 2, "name": "Eric", "email": "eric@example.com"}
]

2๏ธโƒฃ POST ์š”์ฒญ (์ƒˆ ์‚ฌ์šฉ์ž ์ƒ์„ฑ)

POST /users

์š”์ฒญ ๋ณธ๋ฌธ.

{
    "name": "Kobe",
    "email": "kobe@example.com"
}

์‘๋‹ต.

PUT /users/3

3๏ธโƒฃ PUT ์š”์ฒญ (์‚ฌ์šฉ์ž ์ •๋ณด ์—…๋ฐ์ดํŠธ)

PUT /users/3

์š”์ฒญ ๋ณธ๋ฌธ

{
    "name": "Kobe updated",
    "email": "kobe.updated@example.com"
}

์‘๋‹ต

{
    "id": 3,
    "name": "Kobe updated",
    "email": "kobe.updated@example.com"
}

4๏ธโƒฃ DELETE ์š”์ฒญ (์‚ฌ์šฉ์ž ์‚ญ์ œ)

DELETE /users/3

์‘๋‹ต.

{
    "message": "User deleted successfully"
}

5๏ธโƒฃ RESTful API์˜ ์žฅ์ .

1๏ธโƒฃ ํ™•์žฅ์„ฑ.

  • RESTful API๋Š” ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถฐ, ํ™•์žฅ ๊ฐ€๋Šฅํ•˜๊ณ  ์œ ์—ฐํ•œ ์‹œ์Šคํ…œ์„ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2๏ธโƒฃ ํ‘œ์ค€ํ™”.

  • HTTP ํ”„๋กœํ† ์ฝœ์˜ ํ‘œ์ค€์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ, ๋‹ค์–‘ํ•œ ํด๋ผ์ด์–ธํŠธ์™€ ์‰ฝ๊ฒŒ ์—ฐ๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ ๊ฒฝ๋Ÿ‰์„ฑ.

  • JSON ๊ฐ™์€ ๊ฒฝ๋Ÿ‰ ํฌ๋งท์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ ์ „์†ก์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4๏ธโƒฃ ์บ์‹ฑ ๊ฐ€๋Šฅ.

  • HTTP ์บ์‹ฑ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ†ตํ•ด ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ณ  ๋„คํŠธ์›Œํฌ ๋ถ€ํ•˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

6๏ธโƒฃ ๊ฒฐ๋ก .

  • RESTful API๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์ผ๊ด€์„ฑ ์žˆ๊ณ  ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ API ์„ค๊ณ„ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • HTTP ํ”„๋กœํ† ์ฝœ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž์›์„ CRUD ๋ฐฉ์‹์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฉฐ, ๋ฌด์ƒํƒœ์„ฑ, ์ž์› ๊ธฐ๋ฐ˜ ์ ‘๊ทผ, ์ผ๊ด€๋œ ์ธํ„ฐํŽ˜์ด์Šค ๋“ฑ์˜ REST ์›์น™์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.
  • RESTful API๋Š” ๋‹ค์–‘ํ•œ ํด๋ผ์ด์–ธํŠธ(๋ชจ๋ฐ”์ผ, ์›น ๋“ฑ)์™€ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ํ™•์žฅ ๊ฐ€๋Šฅํ•˜๊ณ  ์œ ์—ฐํ•œ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.