npm์ ์ด์ฉํ์ฌ ๋ค์ด๋ก๋ ๊ฐ๋ฅํ HTTP request ๋ชจ๋ ํน์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
postMan์์๋ ์ ๋ถ
"key" : "๊ฐ"
๋ก ๊ฐ์ธ๊ณ
๋ค๋ฅธ๊ณณ์์๋key : '๊ฐ'
ํํ๋ก ์ฒ๋ฆฌ
JSON ์ฌ์ดํธ๋ฅผ ์ด์ฉํ์ฌ fetch๋ฅผ ์ฌ์ฉํ๋ค.
checkGet ํจ์๊ฐ ์คํ๋๋ฉด ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ์ ์๊ฒ ํ์๋ค.
- ์ด๋ ์ฃผ์ํ ์ ์ด ๋ฐ์ response์ data ์์ฑ์ด ํด๋น ๋ถ๋ถ์ด๋ผ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉฐ ํ๋จํ๋ค.
import axios from "axios";
const checkGet = async () => {
try {
const response = await axios.get(
"https://jsonplaceholder.typicode.com/todos/1"
);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
ํด๋น ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฝ์์ ๊ฒฐ๊ณผ๋ก
์์ ๋ง์ฐฌ๊ฐ์ง๋ก 4๊ฐ์ง์ ์์ฑ์ ๊ฐ์ง ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๋ค.
์ด์ ๊ฐ์ ํ์ ์ด์ฉํด postman์ ์ฌ์ฉํ๋ค.
์์ฒญ์ ๋ฐ๋ฅธ ๊ฒฐ๊ณผ๊ฐ ์๋์ ๋ํ๋๋ค
const checkPost = async () => {
try {
const response = await axios.post(
"https://jsonplaceholder.typicode.com/posts",
{
title: "misaka",
body: "mskdaiski",
userId: 1,
}
);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
fetch์ ๋นํด์ (postman ์ ์ธ) body์ JSON.stringify๋ ์๋ถ์ด๊ณ Content-type๋ ์ถ๊ฐํ ํ์๊ฐ ์๋ค.
ํด๋น ๋ฒํผ์ ๋๋ฅด๋ฉด ๊ฒฐ๊ณผ๊ฐ ์ฝ์์ ๋์ค๊ฒ ๋ง๋ค์๋ค.
id๊ฐ 1์ธ๊ณณ์ update ํด์ฃผ์๋ค.
const checkUpdate = async () => {
try {
const response = await axios.put(
"https://jsonplaceholder.typicode.com/posts/1",
{
title: "misaka",
body: "mskdaiski",
userId: 1,
}
);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
๋ง์ฐฌ๊ฐ์ง๋ก axios.put์ ์ด์ฉํ๊ณ ๋ฐ๋ก JSON.stringify , Content-Type์ ์ ํด์ฃผ์ง์๊ณ ์ฒ๋ฆฌํ๋ค.
๋จ์ง delete๋ ์ฌ์ดํธ์์ ์ฌ์ฉํ๋๊ฑฐ๋ผ ๋ณด์ด์ง์๋๋ค๊ณ ํ๋ค. (๋น์ทํ๊ฒ axios.deleteํ๋ฉด ๋ ๊ฑฐ๋ผ ์๊ฐ)
๋๊ฐ์ง axios ์ฌ์ฉ๋ฒ์ด ์๋ค.
axios
.post('url/login', {
email: 'khw970421@kakao.com',
password: 'khw970421',
})
.then((res) => {
console.log(res)
})
์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ url์
๋๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ body ๋ด์ฉ์
์ธ๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ headers ๋ด์ฉ์ ์ด๋ค (ํ์ฌ๋ headers๋ ๋น์ด์๋ค.)
-> ๊ฐ๊ฐ์ ๋งค๊ฐ๋ณ์์ ๊ฐ๊ฐ ์จ์ผํ๋ ๊ฒ์ ํท๊ฐ๋ฆฌ๋ฉด ์๋๋ค.
axios({
method: 'post',
url: 'url/login',
data: {
email: 'khw970421@kakao.com',
password: 'khw970421',
},
headers: { "Content-Type": "application/json" },
}).then((res) => {
console.log(res)
})
์ ์ฒด๋ฅผ
{}
๋ก ๋ฌถ์ด์
method,
url,
body๋ฅผ data์์ ๋ด์ฉ์ผ๋ก ํ๊ณ
headers๋ headers : {...} ํํ๋กํ๋ค.
const getRequest = async (url, options = {}) => {
const returnResult = await axios({
method: 'get',
url: `${API_END_POINT}/${url}`,
...options,
})
.then((response) => response.data)
.catch((error) => {
console.log(error)
})
if (url === 'logout') {
message.success('๋ก๊ทธ์์ ๋์์ต๋๋ค')
removeItem()
}
return returnResult
}
const postRequest = async (url, options = {}) => {
let isProblem
let isAdmin = false
const returnResult = await axios({
method: 'post',
url: `${API_END_POINT}/${url}`,
...options,
})
.then((response) => response)
.catch((error) => {
console.log(error)
})
if (url === 'login') {
removeItem()
setItem('userInformation', returnResult)
if (returnResult.data.user._id === adminId) {
isAdmin = true
}
isProblem = false
return { isProblem: isProblem, isAdmin: isAdmin }
}
return returnResult
}
const putRequest = async (url, options = {}) => {
const returnResult = await axios({
method: 'put',
url: `${API_END_POINT}/${url}`,
...options,
})
.then((response) => response)
.catch((error) => {
console.log(error)
})
return returnResult
}
export { getRequest, postRequest, putRequest }
๊ธฐ์กด์ APIํ์ผ๋ค์ด ๋๋ฌด ๋ง์์
์ด๋ฅผ ํต์ผํด์ ์ฌ์ฉํ๊ธฐ ์ํด
์ง์ Request ํจ์๋ฅผ ๋ง๋ค์๋ค.
์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ url์
๋๋ฒ์งธ ๋งค๊ฐ๋ณ์์์ options๋ก ์ถ๊ฐ์ ์ธ ํ์ํ ๋ด์ฉ๋ค์ ๊ฐ์ ธ์จ๋ค
const { image, _id } = await getRequest('auth-user', {
headers: {
Authorization: BearerToken,
},
})
postRequest('users/upload-photo', {
headers: {
Authorization: BearerToken,
},
data: formData,
})
putRequest('settings/update-password', {
headers: {
'Content-Type': 'application/json',
Authorization: BearerToken,
},
data: {
password: checkPw1,
},
})
url์ ์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก options๋ฅผ ํฌ๊ฒ
{}
๋ก ๋ฌถ์ ๋ด์ฉ๋ค๋ก ๋ฃ๋๋ค
const response = await axios.get(
`https://api.github.com/search/repositories`,
{
params: {
q: keyword,
per_page: 7,
page,
},
},
);
const response = await axios.get(
`https://api.github.com/search/repositories?q=${keyword}&per_page=7&page=${page}`,
);
๋๋ค ๊ฐ์ ๊ฒฐ๊ณผ์ด๋ค.
์ฐธ๊ณ ๋ง ํด๋ผ