울음참고 개발공부
Published 2023. 12. 28. 10:36
TIL | RESTfull API 란? TIL
728x90

 

 

 

RESTfull API 와 일반 API 사이의 주요 차이점에 대한 정리

RESTfull API 만 사용해왔기 때문에 일반 API 를 사용하는 경우와 비교하여 간단하게 비교해보려한다

 

 

 

디자인 패턴 차이 

 

  • RESTful API (Representation State Transfer)

RESTful 은 자원(Resource)을 URI(Uniform Resource Identifier)로 표현하고, 해당 자원에 대한 행위(Verb)를 HTTP 메서드(GET, POST, PUT,DELETE 등)으로 나타내는 아키텍처 스타일이다.

 

RESTful API 는 간결하고 직관적인 디자인을 가진다.

 

 

  • 일반적인 API

RESTful 이 아닌 API 는 다양한 디자인 패턴을 사용할 수 있다.

SOAP(Simple Object Access Protocl)나 GraphQL 등이 RESTful 이 아닌 다른 형태의 API 디자인이다.

 

RESTful 이 아닌 API는 보다 구조화된 메시지와 특정 프로토콜을 사용할 수 있지만 RESTful API 보다 더 복잡하며 기능이 제한될 수 있다. 

 

 

 

 

데이터 형식 차이

 

  • RESTful API

주로 JSON(JavaScript Object Notaion)이나 XML(Extensible Markup Language) 형식을 사용하여 데이터 전송

 

JSON 을 사용하는 예시 ) 

{
    "id": 1,
    "username": "example_user",
    "email": "user@example.com",
    "age": 30,
    "address": {
        "street": "123 Street",
        "city": "City",
        "country": "Country"
    }
}

 

 

 

  • 일반적인  API 

데이터 포맷은 API 디자인에 따라 다를 수 있다.

SOAP 의 경우 주로 XML 을 사용하여 데이터를 전송한다.

 

SOAP/XML 형식 예시 ) 

<user>
    <id>1</id>
    <username>example_user</username>
    <email>user@example.com</email>
    <age>30</age>
    <address>
        <street>123 Street</street>
        <city>City</city>
        <country>Country</country>
    </address>
</user>

 

 

SOAP API 는 HTTP,HTTPS 와 같은 프로토콜을 사용하며, 보통 XML 로 구성된 요청과 응답을 처리한다.

 

SOAP API 를 호출할 때 다음과 같은 구성으로 서버로 전송된다.

 

-> HTTP POST 요청 사용

-> 요청 바디에 XML 형식의 데이터

 

(* SOAP API 에는 WSDL(Web Service Description Language) 파일이 있어 이를 사용해 API의 명세를 확인할 수 있다)

 

 

 

 

조금 더 직관적으로 살펴보자!! 

 

 

 

Ajax 로 호출하는 경우 

 

 

 

Restful API 

 

<!DOCTYPE html>
<html>
<head>
  <title>RESTful API 요청</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

<h2>RESTful API 요청</h2>

<button onclick="sendRestRequest()">RESTful API 호출</button>

<script>
  function sendRestRequest() {
    $.ajax({
      url: 'http://api.example.com/users/123', // RESTful API 엔드포인트
      method: 'GET', // HTTP 요청 메서드
      dataType: 'json', // 응답 데이터 형식 (JSON)
      success: function(response) {
        // 성공적으로 응답을 받았을 때 실행할 코드
        console.log('RESTful API 응답:', response);
      },
      error: function(xhr, status, error) {
        // 요청 실패 시 실행할 코드
        console.error('RESTful API 요청 실패:', status, error);
      }
    });
  }
</script>

</body>
</html>

 

 

 

 

SOAP API 

 

<!DOCTYPE html>
<html>
<head>
  <title>SOAP API 요청</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

<h2>SOAP API 요청</h2>

<button onclick="sendSoapRequest()">SOAP API 호출</button>

<script>
  function sendSoapRequest() {
    $.ajax({
      url: 'http://example.com/soap_service', // SOAP 서버 URL
      method: 'POST', // HTTP 요청 메서드
      contentType: 'text/xml', // 요청 형식 설정
      data: `<?xml version="1.0" encoding="UTF-8"?>
      <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:exam="http://www.example.com">
         <soapenv:Header/>
         <soapenv:Body>
            <exam:GetUserDetails>
               <UserID>123</UserID>
            </exam:GetUserDetails>
         </soapenv:Body>
      </soapenv:Envelope>`,
      success: function(response) {
        // 성공적으로 응답을 받았을 때 실행할 코드
        console.log('SOAP API 응답:', response);
      },
      error: function(xhr, status, error) {
        // 요청 실패 시 실행할 코드
        console.error('SOAP API 요청 실패:', status, error);
      }
    });
  }
</script>

</body>
</html>

 

 

 

 

 


 

 

 

보통 RESTful API 로 작성해왔기 때문에 SOAP API로 작성해본 경험은 없다.

 

추후에 접할 경우가 생기거나 더 배우게된다면 나만의 글로 정리해두자 !!!! 

 

 

 


 

 

[ 참고자료 ]

 

https://aws.amazon.com/ko/what-is/restful-api/

 

RESTful API란 무엇인가요? - RESTful API 설명 - AWS

Amazon API Gateway는 어떤 규모에서든 개발자가 API를 손쉽게 생성, 게시, 유지 관리, 모니터링 및 보안 유지할 수 있도록 하는 완전관리형 서비스입니다. API Gateway를 사용하면 실시간 양방향 통신 애

aws.amazon.com

 

https://aws.amazon.com/ko/compare/the-difference-between-soap-rest/

 

SOAP와 REST 비교 - API 기술 간의 차이점 - AWS

Amazon Web Services(AWS)는 API 요구 사항을 지원하는 Amazon API Gateway를 제공합니다. API Gateway는 어떤 규모에서든 개발자가 API를 손쉽게 생성, 게시, 유지 관리, 모니터링 및 보안 유지할 수 있도록 하는

aws.amazon.com

 

728x90

'TIL' 카테고리의 다른 글

TIL | 7 proxies  (0) 2024.03.29
' i18n ' 이란 ?  (0) 2024.03.12
TIL | HTML - <input> value 에는 null 값을 할당할 수 없다 ?  (1) 2023.11.30
TIL | 요구사항 작성법  (2) 2023.11.13
SQL 예제를 풀어볼 수 있는 사이트들  (0) 2023.11.08
profile

울음참고 개발공부

@메각이

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!