API는 Application Programming Interface로 프로그램(클라이언트)과 프로그램(서버 등)이 정해진 방법으로 데이터를 주고 받는 것이다. API는 주로 서버에서 받아오는 용도로 사용되지만, 컴퓨터 내부에서도 사용될 수 있다. (예 macOS의 metal API 등)
이 부분을 실습하기 위해 data.go.kr에서 js로 api로 데이터를 가져와 보겠다.
우선 사용할 api와 개인 토큰을 발급받는다.
그 다음 js로 다음과 같은 형태로 전송하면 된다.
var serviceKey, returnType, numOfRows, pageNo, year, itemCode, url;
serviceKey = '';
returnType = 'json';
numOfRows = '100';
pageNo = '1';
year = '2020';
itemCode = 'PM10';
url = 'http://apis.data.go.kr/B552584/UlfptcaAlarmInqireSvc/getUlfptcaAlarmInfo'
+'?serviceKey='+serviceKey
+'&returnType='+returnType
+'&numOfRows='+numOfRows
+'&pageNo='+pageNo
+'&year='+year
+'&itemCode='+itemCode;
fetch(url)
.then(response => response.json())
.then(data => {console.log(data)})
이렇게 하면 콘솔에 response가 출력될 것이다.
'HTML, CSS, JS, 웹, 네트워크' 카테고리의 다른 글
| Express, pug (0) | 2025.06.23 |
|---|---|
| Node.js (3) | 2025.06.18 |
| JSON, XML (2) | 2025.06.09 |
| DNS (0) | 2025.06.02 |
| 라우팅 (0) | 2025.06.02 |