๋ฐฑ์ค๋๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ค๋ฉด API๋ฅผ ํธ์ถํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์๋ต๋ฐ๋๋ค. ์ด๋ ์๋ฐ์คํฌ๋ฆฝํธ Web API fetch()ํจ์๋ฅผ ์ฐ๊ฑฐ๋ axios๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
Web API๋ ํด๋ผ์ด์ธํธ ์ธก์์ ์ฌ์ฉํ ์ ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ด์ฅํจ์๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค. ์ค๋ฌด์์๋ ์ด๋ฐ์ ๋ฐ ์ด์ ๋ก axios๋ฅผ ๋ง์ด ์ฌ์ฉํ์ง๋ง, fetchํจ์๋ก๋ ์ด์ง๊ฐํ ๊ธฐ๋ฅ ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค.
fetch('api ์ฃผ์')
.then(res => res.json())
.then(res => {
// data๋ฅผ ์๋ต ๋ฐ์ ํ์ ๋ก์ง
});
์ด๊ฒ์ fetchํจ์์ ๊ธฐ๋ณธ ํํ์ด๋ค. ๊ฐ ๋จ๊ณ๊ฐ ๋ญ ๋ปํ๋์ง ๋ช
ํํ ์์์ผ ํ๋ค.
์ด๊ฒ์ ES5 ํจ์ ์ ์ธ์์ผ๋ก ๋ฐ๊ฟ๋ณด๋ฉด
fetch('api ์ฃผ์')
.then(function(res) {
return res.json();
})
.then(function(res) {
// data๋ฅผ ์๋ต ๋ฐ์ ํ์ ๋ก์ง
});
์ด๋ ๊ฒ ๋์จ๋ค. ๋ณ์์ scope๋ ๊ฐ ํจ์์ด๋ฏ๋ก ์ฒซ ๋ฒ์งธ then์ด๋ ๋ ๋ฒ์งธ then์์ ์๋ res๋ ์๋ก ๋ค๋ฅธ ๊ฒ์ด๋ค.
! res๋ response์ ์ค์๋ง์ด๋ค.
fetch()ํจ์์์ default method๋ get์ด๋ค.
์ค๋ช
: ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
base url: https://api.google.com
endpoint: /user/3
method: get
์๋ตํํ:
{
"success": boolean,
"user": {
"name": string,
"batch": number
}
}
์์ ๊ฐ์ API๋ช ์ธ๋ฅผ ๋ณด๊ณ ์ด๋ป๊ฒ fetch()๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ๊น?
fetch('https://api.google.com/user/3')
.then(res => res.json())
.then(res => {
if (res.success) {
console.log(`${res.user.name}` ๋ ํ์ํฉ๋๋ค);
}
});
API์ฃผ์๋ฅผ ๋ณด๋ user๋ค์ ์๋ 3์ด user id์ธ๊ฒ ๊ฐ๋ค. ๊ณ ์ ๋ API๋ผ๋ฉด ๊ทธ๋ฅ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋์์๋ ๊ณ ์ ํด์ ์ฌ์ฉํ๋ฉด ๋์ง๋ง API์ฃผ์๋ฅผ ์ํฉ์ ๋ง๊ฒ ์ ๋์ ์ผ๋ก ๋ฐ๊ฟ์ค์ผ ํ ๋๊ฐ ์์ฃผ ๋ง๋ค.
๋ฆฌ์กํธ์์ ์ฌ์ฉํ๋ค๊ณ ํ๋ฉด
import React, { useEffect } from 'react';
function User(props) {
useEffect(() => {
const { userId } = props;
fetch(`https://api.google.com/user/${userId}`)
.then(res => res.json())
.then(res => {
if (res.success) {
console.log(`${res.user.name}` ๋ ํ์ํฉ๋๋ค);
}
});
}, []);
...
}
์ด๋ ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
fetch()๊ธฐ๋ณธ์ get์ด๊ธฐ ๋๋ฌธ์ ์๋ฌด๊ฒ๋ ์์ฑํ์ง ์์๋ get์ผ๋ก ํธ์ถํ๋๋ฐ, post์ธ ๊ฒฝ์ฐ์๋ fetch()ํจ์์ method ์ ๋ณด๋ฅผ ์ธ์๋ก ๋๊ฒจ์ค์ผ ํ๋ค.
๋ง์ฝ api๊ฐ get์ธ์ง post์ธ์ง ๋ชจ๋ฅด๋ฉด ๋ฐฑ์ค๋ ๊ฐ๋ฐ์์๊ฒ ๋ฌผ์ด๋ด์ผ ํ๋ค. api์ ๋ณด๋ฅผ ์๋ ๊ฒ์ ์ค๋ก์ง ๊ทธ๊ฒ์ ๋ง๋ ๊ฐ๋ฐ์๋ฟ์ด๋ค.
์ค๋ช
: ์ ์ ๋ฅผ ์ ์ฅํ๋ค.
base url: https://api.google.com
endpoint: /user
method: post
์์ฒญ body:
{
"name": string,
"batch": number
}
์๋ต body:
{
"success": boolean
}
์์๊ฐ์ ๋ช ์ธ๋ฅผ ๋ฐ์ผ๋ฉด
fetch('https://api.google.com/user', {
method: 'post',
body: JSON.stringify({
name: "yeri",
batch: 1
})
})
.then(res => res.json())
.then(res => {
if (res.success) {
alert("์ ์ฅ ์๋ฃ");
}
})
์ด๋ ๊ฒ ๊ตฌํํ๋ค.
get method๋ก ํธ์ถํ๋ ๊ฒ๋ณด๋ค ๋ณต์กํ๊ฑฐ ๊ฐ๋ค.
1. ๋ ๋ฒ์งธ ์ธ์์ method์ body๋ฅผ ๋ณด๋ด์ค์ผ ํ๋ค.
2. method๋ post
3. body๋ JSONํํ๋ก ๋ณด๋ด๊ธฐ ์ํดJSON.stringfy()ํจ์์ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌํ์ฌ JSONํํ๋ก ๋ณํํ๋ค.
fetchํจ์๋ post๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ๋ JSON.stringfy๋ฅผ ํด์ค์ผ ํ๋ ๋ฐ๋ฉด์ axios๋ ๊ฐ์ฒด๋ฅผ ๊ทธ๋๋ก ์์ฑํด๋ ๋๋ ์ฅ์ ์ด ์๋ค. axios๋ ์์ํ ํธ์์ฑ์ ์ ๊ณตํด์ฃผ๊ณ ์์ฒ์ค๊ฐ ์๋ต์ ๋ํ ํ์ฅ์ฑ์๋ ๊ธฐ๋ฅ์ ๋ง๋ค ์ ์๋ค.
path๋ง query string์ผ๋ก ๋๊ฒจ์ค์ผ ํ๋ ์ํฉ์ด ์์ ์๋ ์๋ค. ์ธ์ ๋ path, ์ธ์ ๋ query string์ผ๋ก ํ ์ ์๋ค๋ ๋ง์ด ์๋๋ผ ์์๋๊น ๊ฐ์ API๋ฅผ ์...
์ค์ ๋ก๋ ๋ฐ์ดํฐ ์ ๋ฌ ๋ฐฉ์ ๋ํ ๋ฐฑ์ค๋๊ฐ๋ฐ์์๊ฒ ๋ฌผ์ด๋ด์ผํ๋ค.
์ค๋ช
: ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
base url: https://api.google.com
endpoint: /user
method: get
query string: ?id=์์ด๋
์๋ตํํ:
{
"success": boolean,
"user": {
"name": string,
"batch": number
}
}
ํน๋ณํ ๊ฒ์ ์๊ณ , API์ฃผ์๋ค์ ๋ถ์ฌ์ฃผ๋ฉด ๋๋ค.
fetch('https://api.google.com/user?id=3')
.then(res => res.json())
.then(res => {
if (res.success) {
console.log(`${res.user.name}` ๋ ํ์ํฉ๋๋ค);
}
});
์์ ์ฝ๋์์ ์ฒซ ๋ฒ์งธ then์์ res => res.json()์ด ๋ญ์์๊น?
fetch('https://api.google.com/user', {
method: 'post',
body: JSON.stringify({
name: "yeri",
batch: 1
})
})
.then(res => res.json()) // ์ then์ด ๋๊ฐ๊ณ res.json() ์ ๋ญ์ง?
.then(res => {
if (res.success) {
alert("์ ์ฅ ์๋ฃ");
}
})
์ด๊ฑธ ์ดํดํ๋ ค๋ฉด ์ฐ์ promise์ ๊ฐ๋
์ ์์์ผ ํ์ง๋ง ๋๋ฌด ๊ธธ์ด์ง๋๊น ์๋ตํ๊ฒ ๋ค.
๋์ถฉ ๋ป๋ง ์์๋ณด์!
์ฒซ ๋ฒ์ฌ then์ res๊ฐ ์ด๋ค ๊ฐ์ด ๋ค์ด์ค๊ธธ๋ res.json()์ ๋ฆฌํดํ๋๊ฐ? console.log์ฐ์ด๋ณด๋ฉด ์๊ฒ ์ง ๋ญ...
๊ทผ๋ฐ ์ฝ์์ ์ฐ์ด ๋ณด๋ ค๋ฉด returnํ๋ ํ์ดํ ํจ์์ ๋ฐ๋๋ฅผ ๋ค์ ๋ง๋ค์ด์ค์ผ ํ๋ค.
fetch('https://api.google.com/user', {
method: 'post',
body: JSON.stringify({
name: "yeri",
batch: 1
})
})
.then(res => { // ์ฒซ ๋ฒ์งธ then
console.log(res); // ์ด๋ค ๊ฐ์ด ๋์ค๋์ง ํ์ธํด๋ณด์. ์ค์ ์ ์๋ํ๋ api ์ฃผ์๊ฐ ํ์ํ๋ค.
return res.json();
})
.then(res => { // ๋ ๋ฒ์งธ then
if (res.success) {
alert("์ ์ฅ ์๋ฃ");
}
})
์ฒซ ๋ฒ์งธ thenํจ์์ ์ ๋ฌ๋ ์ธ์ res๋ httpํต์ ์์ฒญ๊ณผ ์๋ต์์ ์๋ต์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด๋ก Response Object๋ผ๊ณ ํ๋ค.
์ฌ๊ธฐ์ ์ฝ์์ ํ์ธํด๋ณด๋ฉด ๋ฐฑ์ค๋์์ ๋๊ฒจ์ฃผ๋ ์๋ต body ์ฆ, ์ค์ ๋ฐ์ดํฐ๋ ๋ณด์ด์ง ์๋๋ค. ๊ทธ๋ง์ {success: true}๋ผ๋ JSON๋ฐ์ดํฐ๋ ์์ ์ฝ๋๋ก๋ ์ฝ์์ ์์ฐํ๋ค๋ ๋ง์ด๋ค.
์๋ต์ผ๋ก ๋ฐ์ JSON๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด Response Object์ jsonํจ์๋ฅผ ํธ์ถํ๊ณ , returnํด์ผ ํ๋ค. ๊ทธ๋ฌ๋ฉด ๋ ๋ฒ์ฌ thenํจ์์์ ์๋ต body์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ ์๋ค.
๊ทธ๋ ๋ค๋ฉด fetch().then().then() ํํ๋ง ๊ธฐ์ตํ๋ฉด ๋ ๊ฒ์ ๊ตฌ๊ตฌ์ ์ ์ค๋ช
ํด๋ดค๋ค.
์ด์ฐจํผ ์๊ณ ์์ด์ผ ํ๋ ๋ถ๋ถ์ด์๊ณ , ๋ฐ๋ก ๋ฐฑ์ค๋์์ ์๋ตbody๋ฅผ ์์ฃผ๋ ๊ฒฝ์ฐ๋ ๋ง๊ธฐ ๋๋ฌธ์ด๋ค.
์๋ตbody๋ก JSON๋ฐ์ดํฐ๋ฅผ ์ฃผ์ง ์๋๋ฐ ํ๋ก ํธ์์ Response Object์ json()์ ํธ์ถํ๋ฉด ์๋ฌ๊ฐ ๋๋ค.
์์๋ฅผ ๋ค๋ฉด
์ค๋ช
: ์ ์ ๋ฅผ ์ ์ฅํ๋ค.
base url: https://api.google.com
endpoint: /user
method: post
์์ฒญ body:
{
"name": string,
"batch": number
}
์๋ต body:
1. ์ ๋๋ก ์ ์ฅํ์ผ๋ฉด status code๋ฅผ 200 ์ ๋ฌ. ์๋ต body๋ ์์
2. ๊ถํ ์ค๋ฅ๊ฐ ์๊ธฐ๋ฉด status code๋ฅผ 403์ผ๋ก ์ ๋ฌํ๊ณ . ์๋ต body๋ ์๋์ ๊ฐ์
{
success: false,
message: "๊ถํ์ด ์์ต๋๋ค"
}
์ด ์ฝ๋๋ฅผ fetch()๋ก ๊ตฌํํด๋ณด๋ฉด
fetch('https://api.google.com/user', {
method: 'post',
body: JSON.stringify({
name: "yeri",
batch: 1
})
})
.then(res => {
if (res.status === 200) {
alert("์ ์ฅ ์๋ฃ");
} else if (res.status === 403) {
return res.json();
}
})
.then(res => {
console.log("์๋ฌ ๋ฉ์์ง ->", res.message);
})
์ด๋ ๊ฒ ๋ง๋ค ์ ์๋ค.