fetch
JavaScript์์ ์ ๊ณตํ๋ API ์ค ํ๋๋ก, ๋คํธ์ํฌ ์์ฒญ์ ๋ง๋ค๊ณ ์๋ต์ ๋ค๋ฃฐ ์ ์๊ฒ ํด์ฃผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. fetch API๋ Promise๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ฉฐ, ๋น๋๊ธฐ์ ์ผ๋ก ๋คํธ์ํฌ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
๋ธ๋ผ์ฐ์ | node | |
์คํ ์ฌ๋ถ |
fetch() ๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ธ๋ผ์ฐ์ ๋ด์ฅ ํจ์๋ก, ๋คํธ์ํฌ์์ ๋ฆฌ์์ค๋ฅผ ๊ฐ์ ธ์ค๋ ๋์์ ๊ฐ๊ฒฐํ๊ฒ ํํํฉ๋๋ค.ย fetch() ํจ์๋:
โข
์ฒซ ๋ฒ์งธ ์ธ์๋ก URL์ ๋ฐ์ต๋๋ค.
โข
๋ ๋ฒ์งธ ์ธ์๋ก ์ต์
๊ฐ์ฒด๋ฅผ ๋ฐ์ต๋๋ค.
โข
Promise ํ์
์ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
๋ฐํ๋ ๊ฐ์ฒด๋:
โข
API ํธ์ถ์ด ์ฑ๊ณตํ์ ๊ฒฝ์ฐ์๋ ์๋ต(response) ๊ฐ์ฒด๋ฅผ resolveํฉ๋๋ค.
โข
์คํจํ์ ๊ฒฝ์ฐ์๋ ์์ธ(error) ๊ฐ์ฒด๋ฅผ rejectํฉ๋๋ค.
fetch() ํจ์๋:
โข
HTTP ํ์ดํ๋ผ์ธ์ ๊ตฌ์ฑํ๋ ์์ฒญ๊ณผ ์๋ต ๋ฑ์ ์์๋ฅผ JavaScript์์ ์ ๊ทผํ๊ณ ์กฐ์ํ ์ ์๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํฉ๋๋ค.
โข
๋คํธ์ํฌ์ ๋ฆฌ์์ค๋ฅผ ์ฝ๊ฒ ๋น๋๊ธฐ์ ์ผ๋ก ์ทจ๋ํ ์ ์์ต๋๋ค.
fetch API์ ๊ธฐ๋ณธ ๊ตฌ์กฐ
fetch(input, init)
.then(response => {
// ์๋ต ์ฒ๋ฆฌ
return response.json(); // JSON ํ์์ผ๋ก ํ์ฑ๋ ๋ฐ์ดํฐ ๋ฐํ
})
.then(data => {
// ํ์ฑ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
console.log(data);
})
.catch(error => {
// ์ค๋ฅ ์ฒ๋ฆฌ
console.error('Request failed:', error);
});
JavaScript
๋ณต์ฌ
fetch API์ ์ฃผ์ ๋งค๊ฐ๋ณ์:
๋งค๊ฐ๋ณ์ | ์ค๋ช
|
url | ์์ฒญ์ ๋ณด๋ผ URL |
options | ์์ฒญ์ ์ต์
๊ฐ์ฒด (๋ฉ์๋, ํค๋, ๋ณธ๋ฌธ ๋ฑ) |
1.
input:
โข
์์ฒญ์ ๋ณด๋ผ URL์ด๋ Request ๊ฐ์ฒด์
๋๋ค.
2.
init (optional):
โข
์ด๊ธฐ ์ค์ ๊ฐ์ฒด๋ก, ์์ฒญ์ ๋ํ ๋ค์ํ ์ต์
์ ์ค์ ํ ์ ์์ต๋๋ค.
โข
๋ฉ์๋(method): GET, POST ๋ฑ HTTP ๋ฉ์๋ ์ค์
โข
ํค๋(headers): ์์ฒญ ํค๋ ์ค์
โข
๋ฐ๋(body): ์์ฒญ ๋ณธ๋ฌธ ์ค์
fetch API์ ๋ฐํ๊ฐ:
fetch ํจ์๋ Promise๋ฅผ ๋ฐํํฉ๋๋ค. ์ด Promise๋ ๋คํธ์ํฌ ์์ฒญ์ ๋ํ ์๋ต(Response)์ ๋ํ๋ด๋ Response ๊ฐ์ฒด๋ฅผ resolveํ๊ฑฐ๋, ์์ฒญ์ด ์คํจํ ๊ฒฝ์ฐ rejectํฉ๋๋ค.
fetch API์ ์ฃผ์ ๋ฉ์๋:
๋ฉ์๋ | ์ค๋ช
|
fetch(input, init) | ๋คํธ์ํฌ ์์ฒญ์ ์์ํ๊ณ Promise๋ฅผ ๋ฐํํฉ๋๋ค. input์ ์์ฒญ URL ๋๋ Request ๊ฐ์ฒด์ผ ์ ์์ผ๋ฉฐ, init์ ์ด๊ธฐ ์ค์ ๊ฐ์ฒด์
๋๋ค. |
clone() | Response๋ Request ๊ฐ์ฒด๋ฅผ ๋ณต์ ํฉ๋๋ค. |
arrayBuffer() | Response๋ฅผ ArrayBuffer๋ก ๋ณํํ๊ณ Promise๋ฅผ ๋ฐํํฉ๋๋ค. |
blob() | Response๋ฅผ Blob๋ก ๋ณํํ๊ณ Promise๋ฅผ ๋ฐํํฉ๋๋ค. |
json() | Response๋ฅผ JSON์ผ๋ก ํ์ฑํ๊ณ Promise๋ฅผ ๋ฐํํฉ๋๋ค. |
text() | Response๋ฅผ ํ
์คํธ๋ก ๋ณํํ๊ณ Promise๋ฅผ ๋ฐํํฉ๋๋ค. |
formData() | Response๋ฅผ FormData๋ก ๋ณํํ๊ณ Promise๋ฅผ ๋ฐํํฉ๋๋ค. |
์์ ์ฝ๋
โข
GET ์์ฒญ
// fetch๋ฅผ ์ฌ์ฉํ์ฌ GET ์์ฒญ ๋ณด๋ด๊ธฐ
fetch('https://httpbin.org/get')
.then(response => {
// ์๋ฒ ์๋ต์ JSON ํ์์ผ๋ก ํ์ฑ
return response.json();
})
.then(data => {
// ํ์ฑ๋ ๋ฐ์ดํฐ ์ถ๋ ฅ
console.log(data);
})
.catch(error => {
// ์ค๋ฅ ์ฒ๋ฆฌ
console.error('Request failed:', error);
});
JavaScript
๋ณต์ฌ
โข
POST ์์ฒญ
// fetch๋ฅผ ์ฌ์ฉํ์ฌ POST ์์ฒญ ๋ณด๋ด๊ธฐ
const url = 'https://httpbin.org/post' // URL ๊ฒฝ๋ก
const data = { name : 'joeun', age : 20 } // ๋ฐ์ดํฐ (๊ฐ์ฒด)
// ์ต์
๊ฐ์ฒด (method, headers, body)
// - method : ์์ฒญ ๋ฉ์๋ (GET, POST, PUT, DELETE)
// - headers : ์์ฒญ ํค๋ ๊ฐ์ฒด
// - body : ์์ฒญ ๋ณธ๋ฌธ (data)
const init = {
method : 'POST',
headers : {
'Content-Type' : 'application/json'
},
body : JSON.stringify(data)
}
fetch(url, init)
.then(response => {
// ์๋ฒ ์๋ต์ JSON ํ์์ผ๋ก ํ์ฑ
return response.json();
})
.then(data => {
// ํ์ฑ๋ ๋ฐ์ดํฐ ์ถ๋ ฅ
console.log(data);
})
.catch(error => {
// ์ค๋ฅ ์ฒ๋ฆฌ
console.error('Request failed:', error);
});
JavaScript
๋ณต์ฌ