문서 목록
⭐️는 필수 지식, 💡는 이해하면 좋은 지식이에요.

REST API

개념 설명

자원을 URI로 표현하고 HTTP 메서드로 조작하는 Representational State Transfer 아키텍처 스타일.

핵심 포인트

  • HTTP 메서드: GET(조회), POST(생성), PUT(전체 수정), PATCH(부분 수정), DELETE(삭제)
  • 상태리스(Stateless): 각 요청은 독립적이며 서버에 상태 저장 안 함
  • 리소스 식별: URI로 리소스를 고유하게 식별 (/users/123)
  • 상태 코드: 200(OK), 201(Created), 400(Bad Request), 401(Unauthorized), 404(Not Found)
  • Content-Type: application/json이 일반적, 요청/응답 모두 JSON 형식

예시 코드

// REST API 클라이언트 구현
class ApiClient {
  constructor(baseURL) {
    this.baseURL = baseURL;
    this.headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    };
  }
  
  async request(endpoint, options = {}) {
    const url = `${this.baseURL}${endpoint}`;
    const config = {
      headers: this.headers,
      ...options
    };
    
    if (config.body && typeof config.body === 'object') {
      config.body = JSON.stringify(config.body);
    }
    
    try {
      const response = await fetch(url, config);
      
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }
      
      const contentType = response.headers.get('content-type');
      if (contentType?.includes('application/json')) {
        return await response.json();
      }
      return await response.text();
    } catch (error) {
      console.error(`API 요청 실패: ${url}`, error);
      throw error;
    }
  }
  
  // CRUD 메서드
  async get(endpoint) {
    return this.request(endpoint, { method: 'GET' });
  }
  
  async post(endpoint, data) {
    return this.request(endpoint, { 
      method: 'POST', 
      body: data 
    });
  }
  
  async put(endpoint, data) {
    return this.request(endpoint, { 
      method: 'PUT', 
      body: data 
    });
  }
  
  async patch(endpoint, data) {
    return this.request(endpoint, { 
      method: 'PATCH', 
      body: data 
    });
  }
  
  async delete(endpoint) {
    return this.request(endpoint, { method: 'DELETE' });
  }
}

더 많은 내용을 확인하세요! 📚

REST API의 전체 내용을 보시려면 프리미엄 플랜이 필요합니다.

✨ 모든 기술 문서 무제한 액세스

📝 실습 예제와 상세한 코드 설명

🎯 면접 예상 질문과 답변 가이드

플랜 비교하기