콘텐츠로 이동

API 사용 예제

REST API 사용 예제를 소개합니다.

Codelab API

목록 조회

curl http://localhost:8080/api/codelabs
const codelabs = await fetch('http://localhost:8080/api/codelabs')
    .then(res => res.json());

생성

curl -X POST http://localhost:8080/api/codelabs \
  -H "Content-Type: application/json" \
  -d '{"title":"Test","description":"Desc","author":"Me"}'
const codelab = await fetch('http://localhost:8080/api/codelabs', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        title: 'Test',
        description: 'Description',
        author: 'Author'
    })
}).then(res => res.json());

WebSocket

const ws = new WebSocket('ws://localhost:8080/api/ws/codelab_id');

ws.onopen = () => {
    ws.send(JSON.stringify({
        type: 'join',
        name: '홍길동'
    }));
};

ws.onmessage = (event) => {
    const data = JSON.parse(event.data);
    console.log('Received:', data);
};

다음 단계