Fetch
๐ Fetch
: ์๊ฒฉ API๋ฅผ ํธ์ถํ ์ ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ด์ฅ ํจ์
๐ Fetch ์ฌ์ฉ๋ฒ
Fetchํจ์๋ ๋๊ฐ์ง์ ์ธ์๋ฅผ ๋ฐ์ต๋๋ค.
- URL
- ์ต์ ๊ฐ์ฒด
์ธ์๋ฅผ ๋ฐ์ Promise๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
๋ฐํ๋ ๊ฐ์ฒด๋ APIํธ์ถ์ด ์ฑ๊ณตํ์ ๋ ์๋ต(response) ๊ฐ์ฒด๋ฅผ resolveํ๊ณ ์คํจํ์ ๋๋ ์์ธ(error)๊ฐ์ฒด๋ฅผ rejectํฉ๋๋ค.
fetch(url, options)
.then((response) => response.json())
.catch((error) => console.log("error:", error));
Response
: HTTP ์๋ต ์ ์ฒด๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด. JSON ๋ณธ๋ฌธ ์ฝํ ์ธ ๋ฅผ ์ถ์ถํ๊ธฐ ์ํด์๋json()
๋ฉ์๋๋ฅผ ํธ์ถํด์ผ ํฉ๋๋ค.
๐ GET ํธ์ถ: ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
fetchํจ์์ ๊ธฐ๋ณธ์ ์ธ ๋์์ GET
์
๋๋ค.
fetch("https://jsonplaceholder.typicode.com/posts/1").then((response) =>
console.log(response)
);
โ ๊ฒฐ๊ณผ
Response {status: 200, ok: true, redirected: false, type: "cors", url: "https://jsonplaceholder.typicode.com/posts/1", โฆ}
์๋ต(response) ๊ฐ์ฒด๋ json๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
json()
์ ํธ์ถํ๋ฉด ์๋ต ๊ฐ์ฒด๋ก๋ถํฐ JSON ํฌ๋ฉง์ ์๋ต์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ก ๋ณํํด์ ์ป์ ์ ์์ต๋๋ค.
// json()์ ํ์ฉํ์ฌ ์๋ต๊ฐ์ฒด๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ก ๋ณํ
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => response.json())
.then((data) => console.log(data));
โ ๊ฒฐ๊ณผ
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipitโตsuscipit recusandae consequuntur โฆstrum rerum est autem sunt rem eveniet architecto"
}
๐ POST ํธ์ถ: ๋ฐ์ดํฐ ์์ฑํ๊ธฐ
POST๋ ์๊ฒฉ API์์ ๊ด๋ฆฌํ๊ณ ์๋ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ ๋ ์ฌ์ฉํฉ๋๋ค.
method
์ต์ ์POST
๋ก ์ง์ ํ๋ค.headers
์ต์ ์ ํตํด JSONํฌ๋ฉง์ ์ฌ์ฉํ๋ค๊ณ ์๋ ค์ค๋ค.- ์์ฒญ ์ ๋ฌธ์ JSON ํฌ๋ฉง์ผ๋ก ์ง๋ ฌํํ์ฌ
body
์ต์ ์ ์ค์ ํด์ค๋ค.
fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title: "Test",
body: "I am testing!",
userId: 1,
}),
}).then((response) => console.log(response));
โ ๊ฒฐ๊ณผ: ์๋ต์ฝ๋๊ฐ 201 Created
Response {type: "cors", url: "https://jsonplaceholder.typicode.com/posts", redirected: false, status: 201, ok: true, โฆ}
// json๋ฉ์๋๋ก ์๋ต๊ฐ์ฒด๋ฅผ ํธ์ถํ์ ๋(response.json())
{title: "Test", body: "I am testing!", userId: 1, id: 101}
๐ PUT, DELETE: ๋ฐ์ดํฐ ์์ , ์ญ์ ํ๊ธฐ
PUT๊ณผ DELETE๋ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ฑฐ๋ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
// PUT
fetch("https://jsonplaceholder.typicode.com/posts/1", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title: "Test",
body: "I am testing!",
userId: 1,
}),
})
.then((response) => response.json())
.then((data) => console.log(data));
โ ๊ฒฐ๊ณผ
{title: "Test", body: "I am testing!", userId: 1, id: 1}
DELETE๋ ๋ณด๋ผ ๋ฐ์ดํฐ๊ฐ ์์ด header
์ body
์ต์
์ด ํ์ํ์ง ์์ต๋๋ค.
// DELETE
fetch("https://jsonplaceholder.typicode.com/posts/1", {
method: "DELETE",
})
.then((response) => response.json())
.then((data) => console.log(data));
โ ๊ฒฐ๊ณผ
{
}
๐ fetch ํจ์ ์ปค์คํ ํ ํ๊ธฐ
async function post(host, path, body, headers = {}) {
const url = `https://${host}/${path}`;
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
...headers,
},
body: JSON.stringify(body),
};
const res = await fetch(url, options);
const data = await res.json();
if (res.ok) {
return data;
} else {
throw Error(data);
}
}
post("jsonplaceholder.typicode.com", "posts", {
title: "Test",
body: "I am testing!",
userId: 1,
})
.then((data) => console.log(data))
.catch((error) => console.log(error));
๐ฌ ์ต์ ๋๊ธ