top of page

WHAT IS API, REST API AND RESTFUL API

Updated: Jan 31


REST API and RESTful API used in the modern state of client/server architecture in software development.





  • What is API?

  • Benefits of using APIs

  • What is a RESTful API and REST API?

  • Endpoints REST API and RESTful APIs.

  • Difference between REST API and RESTful API.

  • REST API vs. RESTful API Usage Scenarios

  • REST API and RESTful API Samples





What is API?


Application Program Interface abbreviated as API which is a software intermediary that allows two applications to talk to each other. Let's look into an example to go through with this topic,





  1. If user wants to book train tickets, web application shows the departure time, stations like that all the informations.


  2. When we look in this from the developer side, developer wants to display all the train schedule informations in the web application, then developer use the APIs to get these train informations from the web server where all the train details and informations are stored.


  3. Then developers can retrieve data from the web server using the APIs to display in the web application.


Benefits Of Using API


APIs needed to make the connection between application functions to handle the data from predefined processes.


  • Ease of integration: APIs can be embedded with any type of software application, So it can easily integrate with the applications and functions between different websites.


  • Reduce software development effort: Using APIs reduce development efforts, developers can implement APIs of Google Maps to provide the exact store location to website visitors.


  • Security: APIs provide a secure communication gateway for different app components to interact and exchange data.



What Is A RESTFUL API And REST API?


REST API














REpresentational State Transfer abbreviated to REST. It is an API that follows a set of rules for an application and services to communicate with each other.


REST APIs work by fielding requests for a resource and returning all relevant information about the resource, translated into a format that clients can easily interpret (this format is determined by the API receiving requests). Clients can also modify i items on the server and even add new items to the server through a REST API.


RESTful API
















The RESTful API obeys the REST architecture constraints and interacts with RESTful web services. The RESTful API also follows the principles of REST API. RESTful APIs are more scalable and have a longer lifespan. The RESTful API uses HTTP requests to access and use data.

















There four basic HTTP requests a client can make are:


GET — To retrieve a resource.


POST — To create a new resource.


PUT — To edit or update an existing resource.


DELETE- To delete a resource.


REST API vs. RESTful API Usage Scenarios


1. When Should We Use REST API?

A REST API is generally used for simple and specific data exchanges. Example Scenarios:

  • Small-scale systems (e.g., an endpoint returning a specific piece of data)

  • Fetching data from external systems (e.g., a weather API)

  • Single-purpose services that do not strictly adhere to architectural rules


2. When Should We Use RESTful API?

A RESTful API fully follows REST principles and is suited for large-scale, flexible systems. Example Scenarios:

  • Microservices architecture

  • APIs managing full CRUD operations

  • Resource-oriented systems (e.g., user management APIs)

  • When OData or similar standards need to be followed

If your API needs to be scalable, modular, and maintainable in the long run, a RESTful API is the better choice.


REST API vs. RESTful API: Key Differences

Feature

REST API

RESTful API

Definition

An API based on REST principles but may not fully adhere to all REST rules.

Fully conforms to REST architecture and follows specific standards.

Standards Compliance

Partial compliance (may not fully use HTTP methods).

Full compliance (resource-based URLs, HTTP methods, stateless structure, etc.).

Data Format

Can be JSON, XML, CSV, etc.

Typically JSON-based.

Scalability

Suitable for smaller projects.

Preferred for large, scalable projects.

Resource Management

May not follow a strict resource-based structure.

Fully resource-based (URI structure reflects resources).

CRUD Operations

May use a single endpoint (e.g., /getUser).

Designed according to HTTP methods (GET, POST, PUT, DELETE).

State Management (Stateful/Stateless)

Can be stateful (may store previous request info).

Must be stateless (each request is independent).

Recommendation: If you're designing an API for long-term scalability and maintenance, RESTful API is the way to go. However, if you need a quick, simple API for data exchange, a REST API may be sufficient.



REST API SAMPLE


METHOD if_http_extension~handle_request.


TYPES : BEGIN OF ty_in_data,

carrid TYPE s_carr_id,

END OF ty_in_data,


BEGIN OF ty_error,

type TYPE char1,

message TYPE char50,

END OF ty_error.


DATA : _request_data TYPE string,

respons_data_ TYPE string,

_s_jsoninput TYPE ty_in_data,

_t_error TYPE STANDARD TABLE OF ty_error,

o_response_ TYPE REF TO if_http_response,

_o_exception TYPE REF TO cx_root.


FIELD-SYMBOLS : <fs_outtab> TYPE ANY TABLE.



_http_server = server.

TRY.

* Parse the Payload in SAP Structure/Table

_request_data = server->request->get_cdata( ).


CALL METHOD /ui2/cl_json=>deserialize

EXPORTING

json = _request_data

pretty_name = /ui2/cl_json=>pretty_mode-camel_case

CHANGING

data = _s_jsoninput.

IF _s_jsoninput IS NOT INITIAL.

* Data Extraction

SELECT * FROM sflight INTO TABLE @DATA(_t_sflight)

WHERE carrid EQ @_s_jsoninput-carrid.

ASSIGN _t_sflight TO <fs_outtab>.

IF sy-subrc IS NOT INITIAL.

* Populate Error Table - for No data found

_t_error = VALUE #( BASE _t_error ( type = 'E' message = 'No data found' ) ).

ASSIGN _t_error TO <fs_outtab>.

ENDIF.

ELSE.

* Populate Error Table - Empty Payload

_t_error = VALUE #( BASE _t_error ( type = 'E' message = 'Empty Payload' ) ).

ASSIGN _t_error TO <fs_outtab>.

ENDIF.


* Output table to Json

IF <fs_outtab> IS ASSIGNED.

respons_data_ = /ui2/cl_json=>serialize(

data = <fs_outtab>

compress = abap_false

pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).


o_response_ = _http_server->response.


o_response_->set_status( code = '200' reason = 'OK' ).

o_response_->set_content_type( 'application/json' ).


o_response_->set_cdata( respons_data_ ).

ELSE.

o_response_->set_status( code = '500' reason = 'Internal Server Error' ).

ENDIF.

CATCH cx_root INTO _o_exception.

_http_server->response->set_status( code = 500 reason = 'Internal Server Error' ).

ENDTRY.

ENDMETHOD.


RESTFUL API Sample

"Create token variable

    CONCATENATE 'Token' gs_company-token INTO lv_token SEPARATED BY space.


"Prepare URL

    url = gs_connection-url  && gs_connection-step_name.


    cl_http_client=>create_by_url(

         EXPORTING  url = url

          IMPORTING client = http_client ).



"Add company code as query to url

    value = gs_company-company_id.

    query = 'company_id'.


    CALL METHOD http_client->append_field_url(

      EXPORTING

        name  = query

        value = value

      CHANGING

        url   = url ).


"update url

    cl_http_client=>create_by_url(


         EXPORTING  url = url


         IMPORTING client = http_client ).


"add method type and content type

    method = gs_connection-mtype.

    http_client->request->set_method( method ).

    http_client->request->set_content_type( 'application/json' ).

    http_client->propertytype_logon_popup = http_client->co_disabled.

"add header for Authorization

    http_client->request->set_header_field(

      EXPORTING

        name  =  'Authorization'

        value =   lv_token ).


"prepare body data

    CALL METHOD zsdrut_json=>data_to_json

      EXPORTING

        data = gt_input

      RECEIVING

        json = out_body.


    IF out_body NE '""'.

      http_client->request->set_cdata( out_body ).

    ENDIF.


"send API

    CALL METHOD http_client->send

      EXCEPTIONS

        http_communication_failure = 1

        http_invalid_state         = 2

        http_processing_failed     = 3

        OTHERS                     = 4.


"get errors

    IF sy-subrc <> 0.

      CALL METHOD http_client->get_last_error

        IMPORTING

          code    = subrc

          message = message.

    ENDIF.


    CALL METHOD http_client->receive

      EXCEPTIONS

        http_communication_failure = 1

        http_invalid_state         = 2

        http_processing_failed     = 3

        OTHERS                     = 4.


    IF sy-subrc <> 0.

      CALL METHOD http_client->get_last_error

        IMPORTING

          code    = subrc

          message = message.

    ENDIF.


"get response

    ls_response = http_client->response->get_cdata( ).


"prepare response data

    CALL METHOD zsdrut_json=>json_to_data

      EXPORTING

        json = ls_response

      CHANGING

        data = lt_out.


"refresh response

    http_client->refresh_response(

      EXCEPTIONS

        http_action_failed = 1

        OTHERS = 2 ).


"close API

    http_client->close(

      EXCEPTIONS

        http_invalid_state = 1

        OTHERS             = 2 ).

35 views0 comments

Recent Posts

See All

댓글


bottom of page