⛳️ 인프런 - cs 지식의 정석 강의를 듣고 학습한 내용입니다.
HTTP 상태코드는 서버가 클라이언트의 요청을 처리한 결과를 숫자로 나타낸다.
상태코드는 1xx, 2xx, 3xx, 4xx, 5xx로 나뉘며, 각 범위는 특정 의미를 가진다.
404 발생iOS 개발에서도 통신을 하면 상태코드를 받아볼 수 있는데,
앞으로 통신 로직을 짤 때 상태코드를 잘 활용해서 디버깅 해봐야겠다는 생각을 했다.
HTTP 메서드는 다음과 같이 여러가지가 있다.


CRUD로 나누면 다음과 같음.
이 중 정리해두지 않으면 헷갈리는 GET과 POST의 차이점과
PUT과 PATCH에 대해 정리!
데이터를 조회(읽기)할 때 사용하는 메서드.
https://search.shopping.naver.com/catalog/33803998618
데이터를 생성할 때 사용하는 메서드.
둘 다 데이터를 수정할 때 사용하지만, 전체 수정(Replace)과 일부 수정(Update)의 차이가 있다.
데이터를 전체 수정(Replace) 할 때 사용.
{
"id": 2,
"email": "kundol@naver.com",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
}
→ PUT 요청 시 위와 같이 모든 필드를 다시 보내야 함.
const PutRequest = () => {
fetch("https://reqres.in/api/users/2", {
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
method: "PUT",
body: JSON.stringify({
"id": 2,
"email": "kundol@naver.com",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
})
}).then(res => res.json())
.then(data => console.log(data))
};
PutRequest();
데이터를 일부 수정(Update) 할 때 사용.
{
"email": "kundol@naver.com"
}
→ PATCH 요청 시 위와 같이 변경할 필드만 보내면 됨.
const PatchRequest = () => {
fetch("https://reqres.in/api/users/2", {
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
method: "PATCH",
body: JSON.stringify({
"email": "kundol@naver.com"
})
}).then(res => res.json())
.then(data => console.log(data))
};
PatchRequest();