wa_ter_ve

[FE] ์›น&์•ฑ ์—ฐํ•ฉ ์Šคํ„ฐ๋”” ๋ณธ๋ฌธ

๊ณต๋ถ€/GDG

[FE] ์›น&์•ฑ ์—ฐํ•ฉ ์Šคํ„ฐ๋””

์ˆ˜win 2024. 11. 18. 10:25

๐Ÿ“Œ ์„ธ์…˜ & ์ฟ ํ‚ค (Session & Cookies)

 

๐Ÿ” ์„ธ์…˜(Session)

: ์„œ๋ฒ„๊ฐ€ ์ผ์ •์‹œ๊ฐ„ ๋™์•ˆ์˜ ์‚ฌ์šฉ์ž ์š”์ฒญ์„ ํ•˜๋‚˜์˜ ์ƒํƒœ๋กœ ๋ณด๊ณ , ํ•ด๋‹น ์ƒํƒœ๋ฅผ ์œ ์ง€ ๋ฐ ์ถ”์ ํ•˜๋Š” ๋ฐฉ์‹. 
(๋ฐฉ๋ฌธ์ž๊ฐ€ ์›น ์„œ๋ฒ„์— ์ ‘์†๋œ ์ƒํƒœ → ์„ธ์…˜)
 
ํด๋ผ์ด์–ธํŠธ์˜ IP ์ฃผ์†Œ๋กœ ๊ฐ ํด๋ผ์ด์–ธํŠธ ์‹๋ณ„์ด ๋ถˆ๊ฐ€ํ•˜๊ธฐ์—,
์„œ๋ฒ„์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‹๋ณ„ํ•˜๋Š” ์šฉ๋„๋กœ session ID๋ฅผ ์‚ฌ์šฉ.

ํŠน์ง•
- ๊ฐ ํด๋ผ์ด์–ธํŠธ์— ๊ณ ์œ  ID๋ฅผ ๋ถ€์—ฌํ•ด, ์„œ๋ฒ„๊ฐ€ ID๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์›€.
- ์„œ๋ฒ„๋Š” ๊ฐ ํด๋ผ์ด์–ธํŠธ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์—ฌ, ๊ฐ ์š”์ฒญ๋“ค์˜ ์„ธ์…˜ ์ •๋ณด๋ฅผ ์œ ์ง€.
- ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ์— ์„ธ์…˜์„ ์ €์žฅํ•˜๋ฏ€๋กœ ์„œ๋ฒ„ ์šฉ๋Ÿ‰์— ์˜ํ–ฅ์„ ๋ฐ›์Œ. ์‚ฌ์šฉ์ž ์ฆ๊ฐ€์‹œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์ฆ๊ฐ€.
- ์„ธ์…˜์€ ์„œ๋ฒ„ ์ธก์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์•ˆ์„ฑ ๋†’์Œ.
- ์„ธ์…˜ ID๋Š” ์ฟ ํ‚ค๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„ ์ „๋‹ฌ๋จ.

 
 

โฌ‡๏ธโฌ‡๏ธโฌ‡๏ธ

 

๐Ÿช ์ฟ ํ‚ค(Cookies)

ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ž‘์€ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ. (์„ธ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ)

ํŠน์ง•
- ์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ ์ธก์— ์ €์žฅ๋จ.
- ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๋Š” ๊ฒฝ์šฐ๋งˆ๋‹ค, ํ•ด๋‹น ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋œ ์ฟ ํ‚ค๋ฅผ ์ „์†ก.

- ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์š”์ฒญ ์‹œ, ๊ทธ ์ฟ ํ‚ค๋ฅผ ํฌํ•จํ•˜์—ฌ ์š”์ฒญ์„ ์ „์†ก. 
- key-value ์Œ์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ๋จ. (ex. session_id = abc123 ) 
- ์š”์ฒญ ์‹œ, ์„œ๋ฒ„~ํด๋ผ์ด์–ธํŠธ ๊ฐ„ ์ž๋™์œผ๋กœ ์ „์†ก๋จ.

 

ex. chrome ๋ธŒ๋ผ์šฐ์ € ์ฟ ํ‚ค

 
 

๐Ÿ“Œ ๋™์ž‘ ๊ณผ์ •

1. ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์„œ๋ฒ„์—๊ฒŒ ์š”์ฒญ
2. ์„œ๋ฒ„๋Š” ์ƒˆ session  ID ์ƒ์„ฑ ๋ฐ ์ฟ ํ‚ค ์ƒ์„ฑ ํ›„, session ID๋Š” ์ฟ ํ‚ค์— ๋‹ด๊ธฐ๊ณ 
3. ์ด ์ฟ ํ‚ค๋Š” http ์ƒํƒœ ์ฝ”๋“œ์— ๋‹ด๊ฒจ ์›น ๋ธŒ๋ผ์šฐ์ €์— ์ „์†ก๋จ.

 
 

์„œ๋ฒ„๊ฐ€ ๋ณด๋‚ธ http ์ƒํƒœ ์ฝ”๋“œ์— ์ฟ ํ‚ค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด,

์›น๋ธŒ๋ผ์šฐ์ €๋Š” ํ•ด๋‹น ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ๋งˆ๋‹ค, ํ•ด๋‹น ์ฟ ํ‚ค๋ฅผ request์— ํฌํ•จํ•จ.
 
 
 

๐Ÿšง HTTPOnly ์ฟ ํ‚ค - (Web)

HTTPOnly ์„ค์ •์ด ๋˜์–ด์žˆ๋Š” ์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ์ธก(Javascript)์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅํ•˜์—ฌ, ์ฟ ํ‚ค๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•  ์ˆ˜ ์—†์Œ.

์ฆ‰, ์˜ค์ง ์„œ๋ฒ„๋งŒ์ด ์ฟ ํ‚ค์— ์ ‘๊ทผ ๊ฐ€๋Šฅ.

 
→  XSS (Cross-Site Scripting) ๊ณต๊ฒฉ ๋ฐฉ์ง€.
๊ณต๊ฒฉ์ž๊ฐ€ ์•…์„ฑ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉ์ž์˜ ์ฟ ํ‚ค๋ฅผ ํƒˆ์ทจํ•˜๋Š” ๊ณต๊ฒฉ.
HttpOnly ์†์„ฑ์ด ์„ค์ •๋œ ์ฟ ํ‚ค๋Š” Javascript์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ํ•ด๋‹น ๊ณต๊ฒฉ์œผ๋กœ๋ถ€ํ„ฐ ์„ธ์…˜ ์ฟ ํ‚ค๋ฅผ ๋ณดํ˜ธ ๊ฐ€๋Šฅ.
 
→  CSRF(Cross-Site Request Forgery) ๊ณต๊ฒฉ ๋ฐฉ์ง€.
์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ๋œ ์›น ์‚ฌ์ดํŠธ ์ƒํƒœ์—์„œ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์„ ๋„์šฉํ•ด ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ๊ณต๊ฒฉ.
HTTP Only ์„ค์ •์„ ํ†ตํ•ด JavaScript์—์„œ ์ฟ ํ‚ค๋ฅผ ์ฝ์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๋ฉด,  CSRF ๊ณต๊ฒฉ์„ ๋ณด๋‹ค ์–ด๋ ต๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ.
์ฃผ๋กœ ์ฟ ํ‚ค์— ํฌํ•จ๋œ ์ธ์ฆ ์ •๋ณด๋ฅผ ์ด์šฉํ•œ ๊ณต๊ฒฉ์ด๋ฏ€๋กœ, ๋ฐฉ์–ด๋ฅผ ์œ„ํ•ด์„œ๋Š” SameSite ์ฟ ํ‚ค ์†์„ฑ๋„ ํ•จ๊ป˜ ์‚ฌ์šฉ.
 

HttpOnly ์ฟ ํ‚ค ์„ค์ • ์˜ˆ์‹œ
- ↓ ์„œ๋ฒ„ ์ธก ์ฝ”๋“œ (Node.js)

const express = require('express');
const app = express();

app.get('/set-cookie', (req, res) => {
  res.cookie('session_id', 'abc123', { httpOnly: true, secure: true }); 
    // httpOnly: true ์„ค์ •์„ ํ†ตํ•ด Javascript(ํด๋ผ์ด์–ธํŠธ) ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋„๋ก.
    // secure๋Š” HTTP ์—ฐ๊ฒฐ์—์„œ๋งŒ ์ „์†ก๋˜๋„๋ก ํ•˜์—ฌ ๋ณด์•ˆ ๊ฐ•ํ™”.
  res.send('Cookie has been set!');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});โ€‹

 

๋”๋ณด๊ธฐ

 SameSite ์ฟ ํ‚ค ์†์„ฑ

- HttpOnly์™€ SameSite ์†์„ฑ์„ ์„ค์ •ํ•˜๋ฉด, XSS์™€ CSRF ๊ณต๊ฒฉ์„ ๋™์‹œ์— ๋ฐฉ์ง€ ๊ฐ€๋Šฅ.

res.cookie('session_id', 'abc123', {
  httpOnly: true,
  secure: true,
  sameSite: 'Strict' // -> SameSite ์†์„ฑ์œผ๋กœ CSRF ๊ณต๊ฒฉ ๋ฐฉ์ง€
});

 


๐Ÿ“Œ HTTP

: HTTP (Hypertext Transfer Protocol) 
ํด๋ผ์ด์–ธํŠธ - ์„œ๋ฒ„ ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „์†ก์„ ์œ„ํ•ด ์ •์˜๋œ ํ”„๋กœํ† ์ฝœ.
์š”์ฒญ(request) & ์‘๋‹ต(response) ๋ฐฉ์‹์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ.
๊ฐ ์‘๋‹ต๊ณผ ์š”์ฒญ์€ header์™€ body๋กœ ๊ตฌ์„ฑ.

 

โ–ท HTTP ์š”์ฒญ (Request)

ํด๋ผ์ด์–ธํŠธ → ์„œ๋ฒ„

<HTTP Method> <URL> <HTTP Version>
<Headers>
<Body (Optional)>
  • <HTTP Method> - ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•˜๋Š” ์ž‘์—…์— ๋Œ€ํ•ด ์ •์˜. (GET, POST, PUT, DELETE ๋“ฑ)
  • <URL> - ์š”์ฒญํ•˜๋Š” ๋ฆฌ์†Œ์Šค ์ฃผ์†Œ. ์„œ๋ฒ„ ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ๊ตฌ์ฒด์ ์ธ ์—”๋“œํฌ์ธํŠธ ์ง€์ •.
  • <Headers>  - ๋ฉ”ํƒ€ ์ •๋ณด ๋ฐ ์š”์ฒญ ์ƒํƒœ๋ฅผ ์„œ๋ฒ„์—๊ฒŒ ์•Œ๋ฆผ.
  • <Body (Optional)> - ์„œ๋ฒ„์— ์ „์†กํ•  ๋ฐ์ดํ„ฐ ๋‚ด์šฉ. (Get ์š”์ฒญ์€ body ์—†์Œ / POST&PUT ์š”์ฒญ์€ body ํฌํ•จ ๊ฐ€๋Šฅ.)

 

๋”๋ณด๊ธฐ

HTTP ์š”์ฒญ ์˜ˆ์‹œ

 

  • GET ์š”์ฒญ
GET /api/user?id=123 HTTP/1.1
Host: example.com
Accept: application/json

 

* /api/user?id=123: ์„œ๋ฒ„์˜ ์—”๋“œํฌ์ธํŠธ ๋ฐ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ

* Accept: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‘๋‹ต ๋ฐ›์„ ๋ฐ์ดํ„ฐ์˜ ์œ ํ˜• 

 

  • POST ์š”์ฒญ
POST /api/user HTTP/1.1
Host: example.com
Content-Type: application/json
Authorization: Bearer token_here

{
  "name": "์ˆ˜์ธ",
  "age": 23,
  "city": "SEOUL"
}

 

* Content-Type: request body์˜ ๋ฐ์ดํ„ฐ ํ˜•์‹. (์œ„ ์˜ˆ์‹œ์—๋Š” JSON ํ˜•์‹์œผ๋กœ ์ •์˜)

* Authorization: ์ธ์ฆ ์ •๋ณด. (์œ„ ์˜ˆ์‹œ์—๋Š” ํ† ํฐ์„ ์‚ฌ์šฉํ•œ ์ธ์ฆ)

* {  ~~~  }: body๋กœ ์„œ๋ฒ„์— ์ „์†กํ•  ๋ฐ์ดํ„ฐ ๋‚ด์šฉ. (์ฃผ๋กœ JSON, XML, ํ…์ŠคํŠธ์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ „์†ก.)

 
 

โ–ท HTTP ์‘๋‹ต(Response)

์„œ๋ฒ„ →  ํด๋ผ์ด์–ธํŠธ 

<HTTP Version> <Status Code> <Status Message>
<Headers>
<Body (Optional)>
  • <HTTP Version> - ์‘๋‹ต์— ์ด์šฉ๋˜๋Š” HTTP ๋ฒ„์ „
  • <Status Code> - ์ƒํƒœ ์ฝ”๋“œ 
  • <Status Message> - ์ƒํƒœ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€
  • <Headers> - ์‘๋‹ต์— ๋Œ€ํ•œ ๋ฉ”ํƒ€ ์ •๋ณด ํฌํ•จ. (ex. Content-Type, Content-Length, Set-Cookie )
  • <Body (Optional)> - ์„œ๋ฒ„์— ์ „์†กํ•  ๋ฐ์ดํ„ฐ ๋‚ด์šฉ. (Get ์š”์ฒญ - ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜ / POST ์š”์ฒญ - ์„ฑ๊ณต ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜)

 

๋”๋ณด๊ธฐ

HTTP ์‘๋‹ต ์˜ˆ์‹œ

 

  • 200 OK ์‘๋‹ต
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 55

{
  "name": "์ˆ˜์ธ",
  "age": 23,
  "city": "SEOUL"
}

 

* 200 OK: ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋จ. 

 

  • 404 Not Found ์‘๋‹ต
HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 138

<html>
  <body>
    <h1>404 Not Found</h1>
    <p>The requested resource could not be found on this server.</p>
  </body>
</html>

 

* 404 Not Found: ์š”์ฒญํ•œ URL์„ ์„œ๋ฒ„์—์„œ ์ฐพ์„ ์ˆ˜ ์—†์Œ.

* Content-Type: ์‘๋‹ต ๋ฐ์ดํ„ฐ ํ˜•์‹ (์œ„ ์˜ˆ์‹œ์—์„œ๋Š” HTML)

* Body: ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ๋ฐ ๋ฐ์ดํ„ฐ๊ฐ€ body์— ํฌํ•จ๋จ.

 

 

โœ”๏ธ

ํด๋ผ์ด์–ธํŠธ๊ฐ€ GET ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด, ์„œ๋ฒ„๋Š” ํ•ด๋‹น ๋ฆฌ์†Œ์Šค๋ฅผ ์กฐํšŒํ•˜์—ฌ ์‘๋‹ต ๋ณธ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•จ.

์‘๋‹ต ๋ณธ๋ฌธ์— ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋Š” ์›น ํŽ˜์ด์ง€, JSON ๊ฐ์ฒด ๋“ฑ์œผ๋กœ ํ‘œํ˜„.

 
 

๐Ÿ“ฎ HTTP ์ƒํƒœ ์ฝ”๋“œ


์ƒํƒœ ์ฝ”๋“œ(response status code) ?

: ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ํ†ต์‹  ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œ์ค€ํ™”๋œ ์ฝ”๋“œ์˜ ๋ชจ์Œ.
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ์„ ๋ณด๋ƒˆ์„ ๋•Œ ์‘๋‹ต๋˜๋Š” ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์—๊ฒŒ ๋ณด๋‚ธ ์š”์ฒญ์ด ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋˜์—ˆ๋Š”์ง€ ํŒŒ์•… ๊ฐ€๋Šฅ.
 
HTTP response ์ฒซ์ค„์— ํ‘œ์‹œ.
(์˜ˆ์‹œ ↓)

HTTP/1.1 200 OK  
Date: Sun, 21 Apr 2013 15:12:46 GMT
Server: Apache
Connection: close
Content-Type: text/html; charset=ISO-8859-1
Content-length: 115
 
<html>
<head>
<title>
A Sample HTML file
</title>
</head>
<body>
The rest of the document goes here
</body>
</html>

 
๐ŸŸข HTTP/1.1 
=> ํ”„๋กœํ† ์ฝœ ๋ฒ„์ „ 
 
๐ŸŸก 200 OK
=> ์ƒํƒœ ์ฝ”๋“œ
 
 

1xx: ์ •๋ณด ์‘๋‹ต
2xx: ์„ฑ๊ณต ์‘๋‹ต
3xx: redirection
4xx: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์˜ค๋ฅ˜
5xx: ์„œ๋ฒ„ ์˜ค๋ฅ˜

 
 
 

โค๏ธ 1XX - ์ •๋ณด ์‘๋‹ต

100 Continue
: request body ์ „์†ก์„ ๊ณ„์†ํ•ด๋„ ๋œ๋‹ค๋Š” ์˜๋ฏธ.
์„œ๋ฒ„๊ฐ€ request header์˜ metadata๋ฅผ ๋ฐ›์€ ์งํ›„, ๋Œ€์šฉ๋Ÿ‰์˜ request body๋ฅผ ๋ฐ›๊ธฐ ์ง์ „์— ๋ณด๋‚ด๋Š” ์‘๋‹ต ์ฝ”๋“œ.
 ๋ถ€๋‹ด์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋Œ€์šฉ๋Ÿ‰์˜ request body๋ฅผ ๋ณด๋‚ด๊ณ  ๋‚˜์„œ๋„ ๊ฑฐ๋ถ€ ๋‹นํ•˜์ง€ ์•Š๊ณ  ๊ณ„์† ์ „์†กํ•ด๋„ ๋˜๋Š”์ง€ ๋ฏธ๋ฆฌ ํ™•์ธ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ์šฉ๋„์˜ ์‘๋‹ต ์ฝ”๋“œ.


101 Switching Protocol
: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ Upgrade ์š”์ฒญ ํ—ค๋”์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ ๋ณด๋‚ด์ง€๋ฉฐ,
์„œ๋ฒ„์—์„œ ํ”„๋กœํ† ์ฝœ์„ ๋ณ€๊ฒฝํ•  ์˜ˆ์ •์ž„์„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์•Œ๋ฆผ.
(ex. HTTP ํ”„๋กœํ† ์ฝœ์—์„œ WebSockets ํ”„๋กœํ† ์ฝœ๋กœ ๋ณ€๊ฒฝ) 


102 Processing (deprecated)
: ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ์ˆ˜์‹ ํ•˜๊ณ  ์ด๋ฅผ ์ฒ˜๋ฆฌ๋Š” ํ•˜๊ณ  ์žˆ์ง€๋งŒ,
์•„์ง ์ œ๋Œ€๋กœ ๋œ ์‘๋‹ต์„ ์•Œ๋ ค์ค„ ์ˆ˜ ์—†์Œ์„ ํด๋ผ์ด์–ธํŠธ์— ์•Œ๋ฆผ.


103 Early Hints
: ์ฃผ๋กœ Link ํ—ค๋”์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ
์„œ๋ฒ„๊ฐ€ ์‘๋‹ต์„ ์ค€๋น„ํ•˜๋Š” ๋™์•ˆ, ์‚ฌ์šฉ์ž ์—์ด์ „ํŠธ๊ฐ€(user agent) ์‚ฌ์ „ ๋กœ๋”ฉ(preloading)์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ.

 
 

๐Ÿ’› 2XX - ์„ฑ๊ณต ์‘๋‹ต

200 OK
: ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋จ.
response body์—๋Š” ์š”์ฒญ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌ.


201 Created
: POST request์— ์˜ํ•ด ์„œ๋ฒ„์— ์ƒˆ ๋ฆฌ์†Œ์Šค๊ฐ€ ์ƒ์„ฑ๋จ. (์ฃผ๋กœ POST ๋˜๋Š” PUT ์š”์ฒญ ์‘๋‹ต)
์ƒˆ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ URL์ด response body์— ์กด์žฌ.


202 Accepted
: ์š”์ฒญ์€ ์ˆ˜์‹ ํ•˜์˜€์œผ๋‚˜, ์•„์ง ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์ง€ ์•Š์Œ.

203 Non-authoritative Information
: ์ด ์‘๋‹ต์€ ์š”์ฒญ๋œ ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ, ๋„คํŠธ์› ํ†ต์‹  ์ค‘๊ฐ„์˜ ์บ์‹œ ์—ญํ• ์„ ํ•˜๋Š” ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋ณด๋‚ธ ์‘๋‹ต.
๋”ฐ๋ผ์„œ ์ด ์‘๋‹ต์€ ์ตœ์‹  ๋ฒ„์ „์ด ์•„๋‹ ์ˆ˜ ์žˆ์Œ.

204 No Content
: ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์—ˆ์œผ๋‚˜, ์‘๋‹ต์œผ๋กœ ๋ณด๋‚ผ ์ž๋ฃŒ ์—†์Œ.

205 Reset Content
: ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์—ˆ์œผ๋‚˜, ์‘๋‹ต์œผ๋กœ ๋ณด๋‚ผ ์ž๋ฃŒ ์—†์Œ.
(์ƒˆ๋กœ๊ณ ์นจ ๋“ฑ์„ ํ†ตํ•ด ์ƒˆ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๋ผ๊ณ  ์•Œ๋ฆผ)


206 Partial Content
: ์„œ๋ฒ„๊ฐ€ GET ์š”์ฒญ์˜ ์ผ๋ถ€๋งŒ ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•จ.
response body์—๋Š” ์š”์ฒญ๋œ ์ผ๋ถ€ ์ž๋ฃŒ๋งŒ ์กด์žฌ.
(ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์–ด๋ฐ›๊ธฐ๋ฅผ ์‹œ๋„ํ•˜๋ฉด, ์„œ๋ฒ„๋Š” request header์˜ Range ํ•ญ๋ชฉ์— ๋ช…์‹œ๋œ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ „์†ก.) 

 
 

๐Ÿ’š 3XX - redirection

300 Multiple Choices
: ์š”์ฒญ์ด ์—ฌ๋Ÿฌ ์‘๋‹ต์ด ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ, ํ•˜๋‚˜๋ฅผ ๊ณจ๋ผ์„œ ๋ณด๋‚ด๋ผ๊ณ  ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์„ ํƒ์ง€๋ฅผ ๋ฐ˜ํ™˜.

301 Moved Permanently
: ์š”์ฒญ๋œ ์ž๋ฃŒ์˜ URL์ด ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์•Œ๋ฆผ.
(๋ณ€๊ฒฝ๋œ URL์€ response header์˜ Location ํ•ญ๋ชฉ์— ์กด์žฌ.)


302 Moved Temporarily
: ์š”์ฒญ๋œ ์ž๋ฃŒ์˜ URL์ด ์ž„์‹œ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์•Œ๋ฆผ.
(์ƒˆ URL์€ response header์˜ Location ํ•ญ๋ชฉ์— ์กด์žฌ.)
์žฌ์š”์ฒญ ์‹œ ์›๋ž˜์˜ URL๋กœ ์š”์ฒญํ•ด์•ผ ํ•จ.


303 See Other
: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋”ฐ๋ฅธ ์ž‘์—…์„ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ URL ํ•„์š”ํ•  ๋•Œ ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†ก.
(์‚ดํŽด๋ณผ URL์€ response header์˜ Location ํ•ญ๋ชฉ์— ์กด์žฌ.)


304 Not Modified
: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ์š”์ฒญ์— request header ํ•ญ๋ชฉ 'If-Modified-Since: ์‹œ๊ฐ' ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ทธ ์‹œ๊ฐ ์ดํ›„๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒฝ์šฐ์—๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญ.
๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•˜์œผ๋ฉด ๋ฐ์ดํ„ฐ๋Š” ๋ณด๋‚ผ ํ•„์š”์—†๊ณ , 304 Not Modified ์ƒํƒœ ์ฝ”๋“œ๋งŒ ์ „์†ก.
์ฃผ๋กœ, ์บ์‹œ ์—ญํ• ์„ ํ•˜๋Š” proxy๊ฐ€ ํ•ด๋‹น ์š”์ฒญ์„ ์ „์†ก.


307 Temporary Redirect
: 302๊ณผ ๋™์ผํ•œ ์˜๋ฏธ (=์š”์ฒญ๋œ ์ž๋ฃŒ์˜ URL์ด ์ž„์‹œ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์•Œ๋ฆผ.)
+ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ HTTP ๋ฉ”์†Œ๋“œ๋„ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€.
(302์™€ ๋‹ค๋ฅธ ์ : ๊ธฐ์กด์˜ HTTP ๋ฉ”์†Œ๋“œ ์œ ์ง€ํ•˜๋ฉฐ redirection)


308 Permanent Redirect
:  301๊ณผ ๋™์ผํ•œ ์˜๋ฏธ (=์š”์ฒญ๋œ ์ž๋ฃŒ์˜ URL์ด ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์•Œ๋ฆผ.)
+ HTTP ๋ฉ”์†Œ๋“œ๋„ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€.
(301๊ณผ ๋‹ค๋ฅธ ์ : ๊ธฐ์กด์˜ HTTP ๋ฉ”์†Œ๋“œ ์œ ์ง€ํ•˜๋ฉฐ redirection)

 
 

๐Ÿฉต 4XX - ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์˜ค๋ฅ˜

400 Bad Request
: HTTP request์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์–ด์„œ, ์„œ๋ฒ„๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์—†์Œ.

401 Unauthorized
: ์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†๋Š” URL ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต ์ฝ”๋“œ. (→ Authorization ์—๋Ÿฌ)

403 Forbidden
: ์ธ์ฆ(Authorization) ๋ฌธ์ œ๊ฐ€ ์•„๋‹Œ, ๋‹ค๋ฅธ ๋ฌธ์ œ๋กœ ์ ‘๊ทผ์ด ๊ธˆ์ง€๋œ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์‘๋‹ต ์ฝ”๋“œ. ( Authentification ์—๋Ÿฌ)
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธ์ฆ๋˜์–ด๋„, ์ ‘๊ทผ์ด ๊ธˆ์ง€๋˜๋Š” ๊ฒฝ์šฐ.
(ex. ๋„คํŠธ์›Œํฌ ํŠธ๋ž˜ํ”ฝ ์ดˆ๊ณผ, IP ์ฐจ๋‹จ, ํŠน์ • ๋ฆฌ์†Œ์Šค ์ œํ•œ, ์ง€์—ญ ์ œํ•œ ๋“ฑ)


404 Not Found
: ์š”์ฒญ๋œ URL์— ๋Œ€ํ•œ ์ž๋ฃŒ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์‘๋‹ต ์ฝ”๋“œ.

405 Method Not Allowed
: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ HTTP ๋ฉ”์„œ๋“œ(POST, PUT, DELETE ๋“ฑ)๊ฐ€ ์„œ๋ฒ„์—์„œ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋ฐ˜ํ™˜.
์„œ๋ฒ„๊ฐ€ ํŠน์ • ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•ด ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ.
(ex. API ์—”๋“œํฌ์ธํŠธ๊ฐ€ GET ์š”์ฒญ๋งŒ์„ ํ—ˆ์šฉํ•˜๊ณ  ์žˆ์„ ๋•Œ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ POST ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ์‘๋‹ต.)


406 Not Acceptable
: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ํ—ค๋”์˜ Accept ํ•ญ๋ชฉ์— MIME ํƒ€์ž…(application/json, text/html, application/xml ๋“ฑ)์— ๋Œ€ํ•ด,
์„œ๋ฒ„๊ฐ€ ๋ฐ์ดํ„ฐ ์ œ๊ณต์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๋ฐ˜ํ™˜.
(ex. ์„œ๋ฒ„๋Š” ์˜ค์ง JSON ํ˜•์‹(application/json)๋งŒ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํด๋ผ์ด์–ธํŠธ๋Š” Accept: text/html์„ ์š”๊ตฌํ•œ ๊ฒฝ์šฐ )


408 Request Timeout
: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ์™„๋ฃŒ(๋งˆ์ง€๋ง‰ ๋นˆ์ค„)๊นŒ์ง€ ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ ค์„œ ์‹œ๊ฐ„ ์ œํ•œ์„ ์ดˆ๊ณผํ–ˆ์Œ์„ ์•Œ๋ฆผ.

410 Gone
: ์š”์ฒญ๋œ URL์— ๋Œ€ํ•œ ์ž๋ฃŒ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Œ.
(์„œ๋น„์Šค๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์„ ๊ฒฝ์šฐ.)


411 Length Required
: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ ์‹œ, request header์— Content-length ํ•ญ๋ชฉ์„ ํฌํ•จํ•˜์ง€ ์•Š์•„, ์„œ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ.

413 Request Entity Too Large
: reqeust body๊ฐ€ ์„œ๋ฒ„๊ฐ€ ํ—ˆ์šฉํ•˜๋Š” ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ดˆ๊ณผ.
(์šฉ๋Ÿ‰์ด ํฐ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ)
 
414 Request URI Tool Long
: URL์ด ๋„ˆ๋ฌด ๊ธธ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ.

415 Unsupported Media Type
: ์„œ๋ฒ„๊ฐ€ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋ฏธ๋””์–ด ํฌ๋งท์„ ์š”์ฒญํ•œ ๊ฒฝ์šฐ.

416 Requested Range Not Satisfiable
: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ํ—ค๋”์˜ Range ํ•„๋“œ๊ฐ€ ์„œ๋ฒ„์˜ ๋ฆฌ์†Œ์Šค ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ.
*(Range header: ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํŠน์ • ๋ฐ์ดํ„ฐ ๋ถ€๋ถ„๋งŒ ๋ฐ›๋„๋ก ์š”์ฒญํ•  ๋•Œ ์‚ฌ์šฉ๋จ)

417 Expection Failed
: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ํ—ค๋”์˜ Expect ํ•„๋“œ๊ฐ€ ์„œ๋ฒ„์—์„œ ์ฒ˜๋ฆฌ ๋ถˆ๊ฐ€๋Šฅ ๋˜๋Š” ์ง€์›๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ.
*(Range header: ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•  ๋•Œ๋งŒ ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•  ๋•Œ ์‚ฌ์šฉ๋จ)

 
 

๐Ÿ’œ 5XX - ์„œ๋ฒ„ ์˜ค๋ฅ˜

500 Internal Server Error
: ์„œ๋ฒ„ ๋‚ด๋ถ€์˜ ์•Œ ์ˆ˜ ์—†๋Š” ์—๋Ÿฌ.

502 Bad Gateway
: ํ”„๋ก์‹œ๋‚˜ ๊ฒŒ์ดํŠธ์›จ์ด ์—ญํ• ์„ ํ•˜๋Š” ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด,
๋‹ค๋ฅธ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์œ ํšจํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ์‘๋‹ต์„ ๋ฐ›์•˜์„ ๊ฒฝ์šฐ.

*(Proxy/Gateway Server: ํด๋ผ์ด์–ธํŠธ์™€ ์‹ค์ œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„œ๋ฒ„ ์‚ฌ์ด์— ์œ„์น˜ํ•˜์—ฌ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ์ค‘๊ณ„.)

503 Service Unavailable
: ์„œ๋ฒ„๊ฐ€ ์ž ์‹œ ์‘๋‹ต ๋ถˆ๊ฐ€๋Šฅ.

504 Gateway Timeout
: ํ”„๋ก์‹œ๋‚˜ ๊ฒŒ์ดํŠธ์›จ์ด ์„œ๋ฒ„๊ฐ€ ๋ฐฑ์—”๋“œ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ,
์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋˜์–ด ํ•ด๋‹น ์„œ๋ฒ„์—์„œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ.
(ex. ๋„คํŠธ์›Œํฌ ์ง€์—ฐ, ๋ฐฑ์—”๋“œ ์„œ๋ฒ„์˜ ๊ณผ๋ถ€ํ•˜, ์„œ๋ฒ„ ๋‹ค์šด ๋“ฑ)

505 HTTP Version Not Supproted
: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์˜ HTTP ๋ฒ„์ „์„ ์„œ๋ฒ„์—์„œ ์ง€์›ํ•˜์ง€ ์•Š์Œ.

511 Network Authentication Required
: ํด๋ผ์ด์–ธํŠธ๊ฐ€ Network ์—‘์„ธ์Šค๋ฅผ ์–ป๊ธฐ ์œ„ํ•œ ์ธ์ฆ์ด ํ•„์š”.
(ex. KT WIFI ์ธ์ฆ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ)

 
 
 


โš™๏ธ API ํ†ต์‹ 

๐Ÿ’ญ API?

= Application Programming Interface
ํ”„๋กœํ† ์ฝœ ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ์†Œํ”„ํŠธ์›จ์–ด ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์„œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜.
(ex. ๋ชจ๋ฐ”์ผ ๋‚ ์”จ์•ฑ์€ API๋ฅผ ํ†ตํ•ด, ๊ธฐ์ƒ์ฒญ์˜ ๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ๋‚ ์”จ ์ •๋ณด๋ฅผ ํ‘œ์‹œ.)
 

์™œ ์‚ฌ์šฉํ• ๊นŒ?

- ๊ฐœ๋ฐœ ๋ฐ ๊ด€๋ฆฌ๊ฐ€ ํšจ์œจ์ 
ํ•ด๋‹น ์„œ๋น„์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์—ˆ๋Š”์ง€ ์ž์„ธํžˆ ๋ชฐ๋ผ๋„ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•จ.
๋”ฐ๋ผ์„œ, ์‹œ๊ฐ„๊ณผ ๋น„์šฉ์„ ์ค„์—ฌ ๋”์šฑ ํšจ์œจ์ ์œผ๋กœ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์คŒ.
- ์œ ์—ฐ์„ฑ ๋ฐ ํ™•์žฅ์„ฑ
์‹ ๊ทœ ๊ฐœ๋ฐœ์— ํ™•์žฅ์„ฑ์ด ์ข‹์Œ. ํƒ€์‚ฌ ์„œ๋น„์Šค์™€ ์—ฐ๊ณ„ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์•ฑ/์›น์„ ๊ฐœ๋ฐœ ๊ฐ€๋Šฅํ•˜์—ฌ,
๋”์šฑ ๋‹ค์–‘ํ•˜๊ณ  ์ƒˆ๋กœ์šด ์„œ๋น„์Šค๋“ค์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์ด ๊ตฌ์ถ•๋จ.
 
 

๐ŸŽˆ 'ํด๋ผ์ด์–ธํŠธ - ์„œ๋ฒ„' ํ†ต์‹ 

- API์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
์šฐ๋ฆฌ๊ฐ€ ์›น ๋˜๋Š” ์•ฑ์„ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ์— ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š”์ง€,
ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ํ†ต์‹ ์ด ์–ด๋–ค ๊ณผ์ •์„ ๊ฑฐ์น˜๋Š”์ง€ ์ดํ•ดํ•ด์•ผํ•จ.

ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ ํ†ต์‹ ์„ ์ปคํ”ผ ์ฃผ๋ฌธ์— ๋น„์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์†๋‹˜ - ์›น ๋ธŒ๋ผ์šฐ์ €(ํด๋ผ์ด์–ธํŠธ)
์„œ๋ฒ„ - ๋ฐ”๋ฆฌ์Šคํƒ€
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค - ์ฐฝ๊ณ 
๋ฐ์ดํ„ฐ - ์›๋‘

 
1. (์†๋‹˜์ด ์ง์ ‘ ์›๋‘๋ฅผ ๊ตฌํ•ด ์ปคํ”ผ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๋ฐ”๋ฆฌ์Šคํƒ€์—๊ฒŒ ์ฃผ๋ฌธ.)
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜์ง€ ์•Š๊ณ  ์„œ๋ฒ„์—๊ฒŒ ์š”์ฒญ.
 
2. (๋ฐ”๋ฆฌ์Šคํƒ€๋Š” ์ฃผ๋ฌธ๋ฐ›์€ ์›๋‘๋ฅผ ์ด์šฉํ•ด ์ปคํ”ผ๋ฅผ ์ œ์ž‘.)
์„œ๋ฒ„๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„์„œ ๊บผ๋ƒ„.
 
3. (์ œ์ž‘ํ•œ ์ปคํ”ผ๋ฅผ ํ•ด๋‹น ๋ฉ”๋‰ด๋ฅผ ์ฃผ๋ฌธํ•œ ์†๋‹˜์—๊ฒŒ ์ „๋‹ฌ.)
์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์š”์ฒญ๋ฐ›์€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ. 
 
 

โ—Ž  API ์ž‘๋™ ๋ฐฉ์‹ 4๊ฐ€์ง€ ์œ ํ˜•

SOAP API
(Simple Object Access Protocol)

- ๋‹จ์ˆœ ๊ฐ์ฒด ์ ‘๊ทผ ํ”„๋กœํ† ์ฝœ ์‚ฌ์šฉ.
- XML์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ๋ฉ”์‹œ์ง€ ๊ตํ™˜.
- ์ฃผ๋กœ HTTP, SMTP์™€ ๊ฐ™์€ ํ”„๋กœํ† ์ฝœ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ.
- ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง€๋ฉฐ(์—„๊ฒฉํ•œ ๊ทœ์น™), ๋ณด์•ˆ, ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ๋“ฑ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ง€์›.
- ๊ณผ๊ฑฐ์— ๋งŽ์ด ์‚ฌ์šฉ๋˜์—ˆ์Œ.
(ex. ๊ฐ•๋ ฅํ•œ ๋ณด์•ˆ๊ณผ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์š”๊ตฌํ•˜๋Š” ๊ธฐ์—… ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ.)

RPC API

(Remote Procedure Call)

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

Websocket API

- JSON ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ตœ์‹  ์›น API.
- ํด๋ผ์ด์–ธํŠธ์•ฑ๊ณผ ์„œ๋ฒ„ ๊ฐ„ ์–‘๋ฐฉํ–ฅ ํ†ต์‹  ์ง€์›. 
- ์„œ๋ฒ„ ์—ฐ๊ฒฐ๋œ ํด๋ผ์ด์–ธํŠธ์— ์ฝœ๋ฐฑ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†ก ๊ฐ€๋Šฅํ•˜์—ฌ REST API ๋ณด๋‹ค ์ƒ๋Œ€์ ์œผ๋กœ ํšจ์œจ์ .

REST API
(Representational State Transfer)

-  REST๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ์— ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” GET, PUT, DELETE ๋“ฑ์˜ ํ•จ์ˆ˜ ์ง‘ํ•ฉ์„ ์ •์˜.
- ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๋Š” HTTP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜.
- REST API์˜ ์ฃผ๋œ ํŠน์ง•์€ 'Stateless' →  ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š์Œ.
- ์„œ๋ฒ„์˜ ์‘๋‹ต์€ ์›น ํŽ˜์ด์ง€์˜ ์ผ๋ฐ˜์ ์ธ ๊ทธ๋ž˜ํ”ฝ ๋ Œ๋”๋ง์ด ์—†๋Š” ์ผ๋ฐ˜ ๋ฐ์ดํ„ฐ.
 

๊ฐ API ๋ฐฉ์‹์˜ ์ฐจ์ด์  ์š”์•ฝ:

์œ ํ˜• ํŠน์ง• ์žฅ์  ๋‹จ์  ์‚ฌ์šฉ ์˜ˆ์‹œ
SOAP API XML์„ ์‚ฌ์šฉ, ์—„๊ฒฉํ•œ ๊ทœ๊ฒฉ ๋ณด์•ˆ, ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ์ง€์› ๋ฌด๊ฒ๊ณ  ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง ๊ธฐ์—… ํ™˜๊ฒฝ, ๊ธˆ์œต ์‹œ์Šคํ…œ
RPC API ์„œ๋ฒ„ ํ•จ์ˆ˜ ํ˜ธ์ถœ, ์›๊ฒฉ ํ”„๋กœ์‹œ์ € ์ฒ˜๋ฆฌ ๊ฐ„๋‹จํ•˜๊ณ  ์ง๊ด€์  ๋„คํŠธ์›Œํฌ ์ง€์—ฐ, ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์–ด๋ ค์›€ ๋ถ„์‚ฐ ์‹œ์Šคํ…œ, gRPC
WebSocket API ์–‘๋ฐฉํ–ฅ ํ†ต์‹ , ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ํ‘ธ์‹œ ๊ฐ€๋Šฅ ์‹ค์‹œ๊ฐ„ ํ†ต์‹  ํšจ์œจ์ , ์ด๋ฒคํŠธ ํ‘ธ์‹œ ๊ฐ€๋Šฅ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„ ์—ฐ๊ฒฐ ์œ ์ง€ ํ•„์š” ์ฑ„ํŒ… ์•ฑ, ์‹ค์‹œ๊ฐ„ ์ฃผ์‹ ๊ฑฐ๋ž˜ ์‹œ์Šคํ…œ
REST API HTTP ๋ฉ”์„œ๋“œ(GET, POST ๋“ฑ), ๋ฌด์ƒํƒœ์„ฑ ๊ธฐ๋ฐ˜ ๊ฐ„๋‹จํ•˜๊ณ  ์œ ์—ฐํ•จ, HTTP ํ”„๋กœํ† ์ฝœ ์‚ฌ์šฉ ์‹ค์‹œ๊ฐ„ ์ฒ˜๋ฆฌ ์–ด๋ ค์›€ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ๋ชจ๋ฐ”์ผ ์•ฑ ํ†ต์‹ 

 

 

โ—Ž  API ๋ณดํ˜ธ

 
1. ์ธ์ฆ ํ† ํฐ
- ์‚ฌ์šฉ์ž์—๊ฒŒ API ํ˜ธ์ถœ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ ๋ถ€์—ฌ์— ์ด์šฉ๋˜๋Š” ์‹œ์Šคํ…œ.
์‚ฌ์šฉ์ž๊ฐ€ ์ž์‹ ์ด ๋ˆ„๊ตฌ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ํ•ด๋‹น ํŠน์ • API ํ˜ธ์ถœ์— ๋Œ€ํ•œ ์•ก์„ธ์Šค ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธ.
(ex. ์›น ์„œ๋ฒ„์— ๋กœ๊ทธ์ธ ์‹œ ํ•ด๋‹น ์›น์˜ ํด๋ผ์ด์–ธํŠธ๋Š” ๋ณด์•ˆ ์—‘์„ธ์Šค๋ฅผ ์œ„ํ•ด ์ธ์ฆ ํ† ํฐ์„ ์‚ฌ์šฉ.)
 
2. API ํ‚ค
- API๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ or ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ™•์ธํ•˜๋Š” ๊ธฐ๋Šฅ ์ˆ˜ํ–‰.
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹๋ณ„ํ•˜๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํŠน์ • API ํ˜ธ์ถœ์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์•ก์„ธ์Šค ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธ.
- ์ธ์ฆ ํ† ํฐ์— ๋น„ํ•ด ์ƒ๋Œ€์ ์œผ๋กœ ์•ˆ์ „์„ฑ์ด ๋–จ์–ด์ง€์ง€๋งŒ, ์‚ฌ์šฉ๋Ÿ‰์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘์„ ์œ„ํ•ด API ๋ชจ๋‹ˆํ„ฐ๋ง ํ—ˆ์šฉ.

 


โ—Ž Open API

์นด์นด์˜ค
https://developers.kakao.com/product
 
๋„ค์ด๋ฒ„
https://developers.naver.com/products/intro/plan/plan.md
 
๊ตฌ๊ธ€
https://console.cloud.google.com/apis/library?supportedpurview=project&pli=1&inv=1&invt=Abhysw
 
 

๐Ÿ’ญ CRUD?

  • Create (POST) - ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์‹œ ์‚ฌ์šฉ. ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์ œ์ถœ.
  • (ex. ์‚ฌ์šฉ์ž๊ฐ€ ํšŒ์›๊ฐ€์ž… ํผ์„ ์ œ์ถœํ•˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์—๊ฒŒ ์ „์†กํ•˜์—ฌ ์„œ๋ฒ„๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž‘์—….)
  • Read (GET) - ๋ฐ์ดํ„ฐ ์ฝ์„ ๋•Œ ์‚ฌ์šฉ. ์กฐํšŒ ๊ธฐ๋Šฅ๋งŒ. ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ ์ˆ˜์ •ํ•˜์ง€ ์•Š์Œ.
  • (ex. ์‚ฌ์šฉ์ž ํ”„๋กœํ•„ ์ •๋ณด ์กฐํšŒ ์‹œ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์—๊ฒŒ GET ์š”์ฒญ์„ ๋ณด๋‚ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์กฐํšŒ ๊ฐ€๋Šฅ.)
  • Update (PATCH) - ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ • ์‹œ ์‚ฌ์šฉ. ์ „์ฒด ๋ฐ์ดํ„ฐ ๋ง๊ณ , ์ผ๋ถ€ ๋ฐ์ดํ„ฐ ์ˆ˜์ • ์‹œ PATCH ์‚ฌ์šฉ.
  • (ex. ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฉ”์ผ์„ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ ์„œ๋ฒ„์— PATCH ์š”์ฒญ์„ ์ „์†ก.)
  • Delete (DELETE) - ๋ฐ์ดํ„ฐ ์‚ญ์ œ ์‹œ ์‚ฌ์šฉ.
  • (ex. ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ ๋˜๋Š” ๊ณ„์ • ํƒˆํ‡ด๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ DELETE ์š”์ฒญ์„ ์„œ๋ฒ„์— ์ „์†ก.)
     

 

๐Ÿ“Œ HTTP ์‘๋‹ต ๋ฉ”์†Œ๋“œ (HTTP request method)

GET ์ž๋ฃŒ ์กฐํšŒ ์š”์ฒญ
POST ์ƒˆ ์ž๋ฃŒ ๋“ฑ๋ก ์š”์ฒญ
PATCH ๊ธฐ์กด ์ž๋ฃŒ ์ˆ˜์ • ์š”์ฒญ
DELETE ์ž๋ฃŒ ์‚ญ์ œ ์š”์ฒญ

 
- ์ฃผ๋กœ ์›น ์„œ๋ฒ„์—์„œ๋Š” GET, POST ๋ฐฉ์‹ ์‚ฌ์šฉ๋จ.
- ์ž๋ฃŒ ์กฐํšŒ ์š”์ฒญ์ผ ๊ฒฝ์šฐ GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ.
- ์ž๋ฃŒ ๋“ฑ๋ก, ์ˆ˜์ •, ์‚ญ์ œ ์š”์ฒญ์ผ ๊ฒฝ์šฐ POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ.
 

GET ์š”์ฒญ ex.

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 ...

 

* /index.html: ์š”์ฒญํ•˜๋Š” ๋ฆฌ์†Œ์Šค (ํŒŒ์ผ).
* Host: ์š”์ฒญํ•˜๋Š” ์„œ๋ฒ„์˜ ๋„๋ฉ”์ธ (www.example.com)
* User-Agent: ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €) ์ •๋ณด

POST ์š”์ฒญ ex.

POST /register HTTP/1.1
Host: www.example.com
Content-Type: application/json
Content-Length: 123

{
  "username": "Sooin",
  "password": "123456789"
}

 

* /register: ์š”์ฒญํ•˜๋Š” ๋ฆฌ์†Œ์Šค (ํšŒ์›๊ฐ€์ž… API)
* Content-Type: ์ „์†กํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ˜•์‹
* Content-Length: ์ „์†กํ•  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด



 
 

๐Ÿ“Œ  API ํ†ต์‹  ์˜ˆ์ œ

๋ฌด๋ฃŒ api ์˜ˆ์ œ ์ œ๊ณต ์‚ฌ์ดํŠธ
https://jsonplaceholder.typicode.com/

(Json ํ˜•์‹ ๋ฐ์ดํ„ฐ)
https://jsonplaceholder.typicode.com/posts

 
Json (JavaScript Object Notation)
: ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ ๊ตํ™˜ ํ˜•์‹.
- ์ฃผ๋กœ, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ ์ „์†ก ์‹œ ์‚ฌ์šฉ๋จ.
- ๋ฐ์ดํ„ฐ์˜ ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ์„ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ํ‘œ๊ธฐ๋ฒ•. ๊ฐ์ฒด ํ˜•ํƒœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ‘œ๊ธฐ๋ฒ•์— ๊ธฐ๋ฐ˜.
Key : Value ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ง.

{
//  ํ‚ค : ๊ฐ’
  "name": "์ˆ˜์ธ",
  "age": 23,
  "city": "SEOUL",
  "fruits" : ["orange", "grape", "peach"]  // ๋ฐฐ์—ด
}

 

์˜ˆ์‹œ

โ—  ํด๋ผ์ด์–ธํŠธ→์„œ๋ฒ„ JSON ์ „์†ก (POST ์š”์ฒญ)

const data = {
  name: "์ˆ˜์ธ",
  age: 23
};

fetch('https://example.com/api', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data));โ€‹


โ—  ์„œ๋ฒ„→ํด๋ผ์ด์–ธํŠธ JSON ๋ฐ˜ํ™˜ (GET ์š”์ฒญ)
fetch('https://example.com/api')
  .then(response => response.json())
  .then(data => {
    console.log(data);
    // JSON ์‘๋‹ต ์˜ˆ์‹œ: { "name": "์ˆ˜์ธ", "age": 23 }
  });โ€‹

* response.json() :  ์„œ๋ฒ„ ์‘๋‹ต body ๋ฐ์ดํ„ฐ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜.

 
 
* API ์ฃผ์†Œ(URL)๋ฅผ ํ†ตํ•ด API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, ์„œ๋ฒ„๋Š” ์œ„์™€ ๊ฐ™์€ Json ๋ฐ์ดํ„ฐ๋“ค์„ ์ „๋‹ฌํ•ด์คŒ.

 


โ–ท axios ์„ค์น˜ (React)

yarn add axios

โ–ท JSON ์„œ๋ฒ„ ์„ค์น˜ (React)

์„ค์น˜
yarn add json-server@0.17.4

์‹คํ–‰
json-server --watch db.json

 

 

๐ŸŸ  axios

 
API ํ˜ธ์ถœ ์ž‘์„ฑ์œ„์น˜
- ์ฃผ๋กœ, ์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ ๋‚ด๋ถ€ ๋˜๋Š” ๋ณ„๋„์˜ ํŒŒ์ผ์— ์ž‘์„ฑ
 
โ—Ž ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ณ  App.tsx ํŒŒ์ผ์— ์•„๋ž˜ ์˜ˆ์ œ ์ฝ”๋“œ ์‹ค์Šตํ•ด๋ณด๊ธฐ

import { useState, useEffect } from 'react';
import axios from 'axios';
import './App.css';

function App() {
  const [posts, setPosts] = useState([]);
  const [newPost, setNewPost] = useState({ title: '', body: '', userId: 1 });

  // GET ์š”์ฒญ 
  useEffect(() => { //useEffect๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ๋  ๋•Œ ์ž๋™์œผ๋กœ GET ์š”์ฒญ ์‹คํ–‰๋จ.
    axios.get('https://jsonplaceholder.typicode.com/posts')
      .then(response => {
        console.log(response.data);
        setPosts(response.data); // ๋ฐ์ดํ„ฐ๋ฅผ ์ƒํƒœ์— ์ €์žฅ
      })
      .catch(error => {
        console.error('Error fetching data:', error);
      });
  }, []);

  // POST ์š”์ฒญ
  const handlePost = () => { //๋ฒ„ํŠผ ํด๋ฆญ์‹œ ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ๋  ๋•Œ ์ž๋™์œผ๋กœ GET ์š”์ฒญ ์‹คํ–‰๋จ.
    axios.post('https://jsonplaceholder.typicode.com/posts', newPost)
    // then/catch๋ฅผ ํ†ตํ•œ ์‘๋‹ต ์ฒ˜๋ฆฌ ํ™•์ธ
      .then(response => { 
        console.log(response.data);
        alert('Post created successfully');
      }) // ์„ฑ๊ณต
      .catch(error => {
        console.error('Error creating post:', error); // ์—๋Ÿฌ ํ•ธ๋“ค๋ง(์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ)
      }); // ์‹คํŒจ
  };

  return (
    <div>
      <h1>React Axios Example</h1>
      <h2>Posts</h2>
      <ul>
        {posts.map((post: any) => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>

      <h2>Create New Post</h2>
      <input
        type="text"
        placeholder="Title"
        value={newPost.title}
        onChange={(e) => setNewPost({ ...newPost, title: e.target.value })}
      />
      <textarea
        placeholder="Body"
        value={newPost.body}
        onChange={(e) => setNewPost({ ...newPost, body: e.target.value })}
      ></textarea>
      <button onClick={handlePost}>Create Post</button> 
      // ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ POST ์š”์ฒญ์œผ๋กœ ์„œ๋ฒ„๋กœ ์ „์†ก
    </div>
  );
}

export default App;

 
 
 

๐ŸŸก fetch

- fetch: ๋ธŒ๋ผ์šฐ์ € ๋‚ด์žฅ ํ•จ์ˆ˜
๋„คํŠธ์›Œํฌ ์š”์ฒญ ์‹œ ์‚ฌ์šฉ.

import { useState, useEffect } from 'react';

function Posts() {
  const [posts, setPosts] = useState([]); //posts: API๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์ €์žฅ
  const [loading, setLoading] = useState(true); //loading: ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์ƒํƒœ ์•Œ๋ฆผ
  const [error, setError] = useState(null); //error: ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ, ํ•ด๋‹น ๋ฉ”์‹œ์ง€ ์ €์žฅ

  useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/posts')
      .then(response => {
        if (!response.ok) {
          throw new Error('Network response was not ok');
        }
        return response.json();
      })
      .then(data => {
        setPosts(data); //loading์ด true๋ฉด ๋กœ๋”ฉ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
        setLoading(false);
      })
      .catch(error => {
        setError(error.message); //loading์ด false๋ฉด ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
        setLoading(false);
      });
  }, []); // ๋นˆ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜์Œ ๋งˆ์šดํŠธ๋  ๋•Œ๋งŒ ์‹คํ–‰

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {error}</p>;

  return (
    <div>
      <h1>Posts</h1>
      <ul>
        {posts.map(post => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>
    </div>
  );
}

export default Posts;

 
 
↓ ↓ ↓ ↓
 

โœ… ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ

API ํ˜ธ์ถœ์„ async์™€ await์„ ์ด์šฉํ•ด ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค๋ฉด ๊ฐ€๋…์„ฑ์ด ํ–ฅ์ƒ๋˜์–ด,
์ฝ”๋“œ๋“ค์ด ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š” ์ฝ”๋“œ์ธ์ง€, ์ฝ”๋“œ์˜ ์‹คํ–‰์ˆœ์„œ๋ฅผ ์ง๊ด€์ ์œผ๋กœ ํ™•์ธ ๊ฐ€๋Šฅ + ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋„ ์šฉ์ด.

useEffect(() => {
  const fetchPosts = async () => {
    try {
      const response = await fetch('https://jsonplaceholder.typicode.com/posts');
      if (!response.ok) {
        throw new Error('Network response was not ok');
      }
      const data = await response.json();
      setPosts(data);
      setLoading(false);
    } catch (error) {
      setError(error.message);
      setLoading(false);
    }
  };

  fetchPosts();
}, []);

 
 
 

๐Ÿ”ต http 

const https = require('https');

https.get('https://jsonplaceholder.typicode.com/posts', (res) => {
  let data = '';
  
  res.on('data', (chunk) => {
    data += chunk;
  });
  
  res.on('end', () => {
    console.log(JSON.parse(data));
  });
  
}).on('error', (err) => {
  console.error(err.message);
});

 
 
 

๐ŸŸฃ dio

import 'package:dio/dio.dart';

void main() async {
  final dio = Dio();

  // GET ์š”์ฒญ
  try {
    Response response = await dio.get('https://jsonplaceholder.typicode.com/posts');
    print(response.data);
  } catch (e) {
    print('Error fetching data: $e');
  }

  // POST ์š”์ฒญ
  try {
    Response response = await dio.post(
      'https://jsonplaceholder.typicode.com/posts',
      data: {
        'title': 'test',
        'body': 'blahblah',
        'userId': 1,
      },
    );
    print('${response.data}');
  } catch (e) {
    print('Error posting data: $e');
  }
}

 


๐Ÿ“‚ ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ

npm
https://www.npmjs.com/search?q=express
 
headers๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์ •ํ•  ์ˆ˜ ์—†๋Š” ํ—ค๋” ๋ชฉ๋ก.
https://fetch.spec.whatwg.org/#forbidden-header-name


๐Ÿ“š ์ฐธ๊ณ  ์ž๋ฃŒ

https://dev-coco.tistory.com/98
https://aws.amazon.com/ko/what-is/api/
https://brunch.co.kr/@operator/65
https://gitbook-js.hyobb.com/section4/4-4
https://ko.javascript.info/fetch
 
 

'๊ณต๋ถ€ > GDG' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ  (0) 2024.10.30
Javascript 2 DOM  (5) 2024.10.07
GDG-WEB 2์ฃผ์ฐจ ๊ณผ์ œ  (0) 2024.10.01
Javascript 1-4 ์—ฐ์‚ฐ์ž  (0) 2024.09.30
Javascript 1-3 ์ œ์–ด๋ฌธ  (0) 2024.09.30