RESTful API
728x90

'Restful API๋ž€?' ์ด๋ผ๋Š” ์งˆ๋ฌธ์— ๋‹ต๋ณ€์„ ์œ„ํ•œ REST API์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด๋ณธ๋‹ค.

RESTful API๋Š” ๊ฒฐ๊ตญ RESTํ•˜๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ, ๋จผ์ € REST๊ฐ€ ์–ด๋–ค ์˜๋ฏธ์ธ์ง€๋ถ€ํ„ฐ ์•Œ์•„์•ผํ•  ํ•„์š”์„ฑ์ด ์žˆ๊ฒ ๋‹ค.

 

REST API

REST API๋Š” HTTP๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์˜ ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์„ ๊ทœ์ •ํ•œ ์•„ํ‚คํ…์ฒ˜

REST๋Š” REpresentational State Transfer์˜ ์•ฝ์ž๋กœ, ์ž์›(Resource)์— ๋Œ€ํ•œ ์š”์ฒญ์„ ํ–‰์œ„(HTTP Method)๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

์ฃผ๋กœ JSON, XML ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ˜ํ™˜๋˜์–ด ์ฒ˜๋ฆฌ๋œ๋‹ค.

 

REST์˜ ๊ตฌ์„ฑ

  • ์ž์›(Resource) - URI (์—”๋“œํฌ์ธํŠธ)
  • ํ–‰์œ„(Verb) - HTTP Method (HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ)
  • ํ‘œํ˜„(representations) - ํŽ˜์ด๋กœ๋“œ (JSON,XML ๋“ฑ์˜ ํฌ๋งท์„ ํ†ตํ•ด ์ „๋‹ฌ)

 

REST์˜ ํŠน์ง•

  • Stateless(๋ฌด์ƒํƒœ์„ฑ): ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. (์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ๋งŒ์„ ์ฒ˜๋ฆฌ)
  • Cacheable(์บ์‹œ ๊ฐ€๋Šฅ): ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋ฒ„์˜ ์‘๋‹ต์„ ์บ์‹ฑ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Uniform Interface(์ผ๊ด€๋œ ์ธํ„ฐํŽ˜์ด์Šค): ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์„ค๊ณ„๋˜์–ด์•ผ ํ•œ๋‹ค.
  • Layered System(๊ณ„์ธต ๊ตฌ์กฐ): ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋ฒ„์™€ ์ง์ ‘ ํ†ต์‹ ํ•˜๋Š” ๋Œ€์‹  ์ค‘๊ฐ„ ๊ณ„์ธต(๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ, ์บ์‹œ ๋“ฑ)์„ ํ†ตํ•ด ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Code on Demand(์„ ํƒ์  ์ฝ”๋“œ ์ „์†ก): ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ๋กœ ์‹คํ–‰ ์ฝ”๋“œ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

REST ์„ค๊ณ„ ์›์น™

  • HTTP URI๋ฅผ ํ†ตํ•ด ์ž์›์„ ๋ช…์‹œ
  • HTTP ๋ฉ”์„œ๋“œ(GET,POST,PUT,DELETE)๋ฅผ ํ†ตํ•ด ํ–‰์œ„ ํ‘œํ˜„

์œ„ ๋‘ ๊ฐ€์ง€๊ฐ€ ์ž˜ ์ง€์ผœ์กŒ์„ ๋•Œ RESTfulํ•˜๊ฒŒ ์„ค๊ณ„๋˜์—ˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

RESTful API

์œ„์— ๋‚˜์—ดํ•œ REST์˜ ๊ธฐ๋ณธ ์›์น™์„ ์ž˜ ์ง€ํ‚จ API๋ฅผ RESTful API๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.

 

 

HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ

์–ด๋”˜์ง€ ์ต์ˆ™ํ•œ GET, POST ๋“ฑ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋“ค!..

๋ฉ”์„œ๋“œ ์ข…๋ฅ˜ ๋ชฉ์  ํŽ˜์ด๋กœ๋“œ ํ•„์š”์—ฌ๋ถ€
GET index/retrieve ๋ชจ๋“ /ํŠน์ • ๋ฆฌ์†Œ์Šค ์ทจ๋“ x
POST create ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ o
PUT replace ๋ฆฌ์†Œ์Šค์˜ ์ „์ฒด ๊ต์ฒด o
PATCH modify ๋ฆฌ์†Œ์Šค ์ผ๋ถ€ ์ˆ˜์ • o
DELETE delete ๋ชจ๋“ /ํŠน์ • ๋ฆฌ์†Œ์Šค ์‚ญ์ œ x

PUT๊ณผ PATCH ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋‘˜ ๋‹ค ์ˆ˜์ •์— ์ด์šฉ๋  ์ˆ˜ ์žˆ์ง€๋งŒ,
PUT ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋ฆฌ์†Œ์Šค์˜ ์ „์ฒด๋ฅผ, PATCH๋Š” ์ผ๋ถ€๊ฐ€ ์ˆ˜์ •๋œ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

 

 

RESTful API ์ž‘์„ฑ ์˜ˆ์‹œ

  • URI์— ๋™์‚ฌ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  HTTP Method๋กœ ๋Œ€์ฒดํ•˜๊ธฐ
# bad
GET /todos/delete/1
GET /deleteTodos/1

# good
DELETE /todos/1
  • ์†Œ๋ฌธ์ž๋กœ ๊ตฌ์„ฑํ•˜๊ธฐ
# bad
/Todos/1

# good
/todos/1
  • ํŒŒ์ผ ํ™•์žฅ์ž ํ‘œ์‹œํ•˜์ง€ ์•Š๊ธฐ
# bad
users/1/profile.png

# good
users/1/profile (Acceipt: image/png)

 

 

Restful API ์™ธ์˜ ๋‹ค๋ฅธ API ์•„ํ‚คํ…์ฒ˜ ์Šคํƒ€์ผ

SOAP API(Simple Object Access Protocol)

SOAP๋Š” XML ๊ธฐ๋ฐ˜์˜ ๋ฉ”์‹œ์ง€ ๊ตํ™˜ ํ”„๋กœํ† ์ฝœ์ด๋‹ค.
SOAP API๋Š” HTTP, HTTPS, SMTP ๋“ฑ์˜ ํ”„๋กœํ† ์ฝœ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋ฉฐ, ์ฃผ๋กœ XML ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

GraphQL API(Graph Query Language)

GraphQL์€ ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœํ•œ ์ฟผ๋ฆฌ ์–ธ์–ด๋‹ค.
GraphQL API๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ์š”์ฒญํ•œ ํ•„๋“œ๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ์—ฌ๋Ÿฌ ์š”์ฒญ์„ ๋ฌถ์–ด ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ ๋“ฑ์„ ์ œ๊ณตํ•œ๋‹ค.

์ด์™ธ์—๋„ RPC API, WebSocket API ๋“ฑ์ด ์žˆ๋‹ค.

 

 

์ถœ์ฒ˜

https://aws.amazon.com/ko/what-is/restful-api/
https://pronist.dev/146#%EC%A0%95%EB%A0%AC%2C%20%ED%95%84%ED%84%B0%EB%A7%81%2C%20%ED%95%84%EB%93%9C%20%EC%84%A0%ED%83%9D-1

https://blog.bytebytego.com/p/why-is-restful-api-so-popular

728x90