[TIL] HTTP, Fetch

Dev_minยท2019๋…„ 9์›” 27์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
6/61

๐Ÿ‘‰HTTP ๋ฉ”์†Œ๋“œ๋ž€?

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์‚ฌ์ด์— ์ด๋ฃจ์–ด์ง€๋Š” ์š”์ฒญ(Request)๊ณผ ์‘๋‹ต(Response) ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ๋ฐฉ์‹

1) HTTP ์š”์ฒญ ํ—ค๋” ํฌ๋งท

ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๋Š” ๋ฉ”์‹œ์ง€์ด๋ฉฐ, ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ
[Request Line][ Header ]
[(white space)][ Body ]

2) HTTP ์‘๋‹ต ํ—ค๋” ํฌ๋งท

์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•ด ์‘๋‹ตํ•˜๋Š” HTTP ์‘๋‹ตํ—ค๋”์˜ ๊ตฌ์กฐ
[ Status Line ][ Header ]
[(white space)][ Body ]

3) HTTP ๋ฉ”์†Œ๋“œ

1) GET : URI ์— ํ•ด๋‹นํ•˜๋Š” ์ •๋ณด์˜ ์ „์†ก์š”์ฒญ์„ ๋ณด๋ƒ„
2) POST: ์„œ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ๋ณด๋ƒ„
3) PUT: ์ž๋ฃŒ๋ฅผ ์ „์†กํ•˜์—ฌ ํ•ด๋‹น URI ์— ์ž๋ฃŒ๋ฅผ ์ €์žฅ
4) DELETE: ํ•ด๋‹น URI ์˜ ์ž์›/์ •๋ณด๋ฅผ ์‚ญ์ œ
5) HEAD: URI ์— ํ•ด๋‹นํ•˜๋Š” ์ •๋ณด์˜ ์ „์†ก์„ ์š”์ฒญ. GET ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ Meta ์ •๋ณด๋งŒ์„ ์š”์ฒญ

๐Ÿ‘‰Fetch API

๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‰ฝ๋„๋ก ๋„์™€์ฃผ๋Š” ๊ฒƒ

	// POST ์˜ˆ์‹œ
    // This is the url you should use to communicate with the AWS server.
    const serverURL = 'http://52.78.206.149:3000/messages'
    window.fetch(serverURL, {
      method: 'POST',
      body: JSON.stringify(message),
      headers: {
        "Content-Type": "application/json",
      }
    }).then(response => {
      return response.json();
    }).then(json => {
      console.log(json);
      // message sent!
    });

๐Ÿ‘‰ HTML๊ณผ Javascript๊ฐ„ ์†Œํ†ต, fetch๋ฅผ ์ด์šฉํ•ด์„œ ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ์‹คํ–‰. ์•„์ง POST๋ถ€๋ถ„์—์„œ body, headers ๋ถ€๋ถ„์˜ ์ดํ•ด๊ฐ€ ๋” ํ•„์š”ํ•˜๋‹ค.

profile
TIL record

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