API & fetch

서이·2023년 10월 6일
0

개념정리

목록 보기
9/9

API(Application Programming Interface)

 서로 다른 어플리케이션(프로그램)이 데이터를 주고 받을 수 있도록 도와주는 인터페이스(매개체)

REST API

웹 서비스를 개발할 때 가장 범용적으로 사용되는 API
HTTP프로토콜을 기반으로 동작함
요청하려는 데이터를 http로 시작하는 주소로 표현함

(요청하려는 서버의주소)/(요청하려는 데이터)

  HTTP Method : 서버가 데이터를 어떻게 처리해주었으면 좋겠는지 설정함

  • GET :데이터 불러오기
  • POST : 데이터 생성하기
  • PUT : 데이터 수정하기
  • DELETE : 데이터 삭제하기

https://jsonplaceholder.typicode.com/ 오픈API
Resources 에서 posts 더미데이터 가져오기

let response = fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => console.log(res));

//fetch는 API호출을 하게 도와주는 내장함수이다.

👉async function getDate(){
  let rawResponse = awit fethch('https://jsonplaceholder.typicode.com/posts');
  let jsonResponse = await rawRespons.json();
  console.log(jsonResponse);
}
getData();

/*promise의 then, async await는 용도는 같지만, 간결성, 에러 핸들링, 에러 위치 
확인 측면에서 차이가 있음. 이외에도 async await은 디버그를 할 때 
then과 달리 정확한 위치를 지정할 수 있는 장점이 있다.*/

Reference
https://www.udemy.com/course/react-next-master/learn/lecture/39610782#reviews

profile
작성자 개인이 잊을 때마다 보라고 정리한 글

0개의 댓글