๐ŸŒฑ REST API!

sh_awtylol4ยท2022๋…„ 7์›” 12์ผ
0

๐ŸŒฑ Today I Learn

๋ชฉ๋ก ๋ณด๊ธฐ
9/16
post-thumbnail

๐Ÿค” 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 ์„ค๊ณ„ ๊ธฐ๋ณธ ๊ทœ์น™

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

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

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

  • 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์ธ students๋ฅผ ์‚ญ์ œํ•˜๋Š” route: DELETE/students/12

๐Ÿ“Ž REST API ์„ค๊ณ„ ๊ทœ์น™

1. ์Šฌ๋ž˜์‹œ ๊ตฌ๋ถ„์ž(/)๋Š” ๊ณ„์ธต ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.

Ex) https://restapi.example.com/house/apartments

2. URI ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋กœ ์Šฌ๋ž˜์‹œ(/)๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.

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

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

Ex) http://restapi.example.com/houses/apartments/ (X)

3. ํ•˜์ดํ”ˆ(-)์€ URI ๊ฐ€์กฑ์„ฑ์„ ๋†’์ด๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค

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

4. ๋ฐ‘์ค„(_)์€ URI์— ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

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

5. URI ๊ฒฝ๋กœ์—๋Š” ์†Œ๋ฌธ์ž๊ฐ€ ์ ํ•ฉํ•˜๋‹ค.

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

  • RFC 3986(URI ๋ฌธ๋ฒ• ํ˜•์‹)์€ URI ์Šคํ‚ค๋งˆ์™€ ํ˜ธ์ŠคํŠธ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜๋„๋ก ๊ทœ์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

6. ํŒŒ์ผํ™•์žฅ์ž๋Š” 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)

7. ๋ฆฌ์†Œ์Šค ๊ฐ„์—๋Š” ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ

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

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

hjhgjhg

profile
ํฌ๊ธฐํ•˜๋Š” ๊ทธ ์ˆœ๊ฐ„์ด ๋ฐ”๋กœ ์‹œํ•ฉ์ข…๋ฃŒ์˜ˆ์š”...

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