Saturday, 14 May 2022

ABAP REST API http Service call from Postman: JSON to ABAP data

This blog post will describe the steps of how to expose REST API and handle REST API request from SAP ECC 6.0 without installing SAP Gateway service builder component. It will also help to convert json data to ABAP data. In this blog post I have described how to build web server service in ECC application server of Rest APIs using JSON data format.

Prerequisite JSON data format, SAP logon and postman.

◉ Define JSON structure:

JSON is a text-based data format which follow JavaScript object syntax. It stores the value in form of key value pair. Each of the property and value is separated with colon (:).

In ABAP define JSON structure which will be posted though POST request body and will be handled by HANDLE_REQUEST method. In se11 create JSON structure.

JSON structure and Table Type:

◉ Implement http Request handle Method inside handling class:

Create a class ZMG_TEST_JSON with public instantiation using t-code se24 and add HANDLE_REQUEST standard method.

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

Inside the class, Implement HANDLE_REQUEST standard method of interface IF_HTTP_EXTENSION. Implemented method will process POST request with JSON body which will be posted through postman. Request can also be triggered from AZURE or any other third-party.

◉ Maintain Service: ICF Hierarchy service:

Internet Communication Framework (ICF) services is an integrated component of Application server which allows us to communicate with SAP systems using internet standard protocol HTTP, HTTPs SMTP etc.

SICF t-code to create independent service ZMG_TEST_SRV in ICF hierarchy.

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

To link handler class with created service, in service’s hander list tab add created handler class (ZMG_TEST_JSON) and save.

Adding ZMG_TEST_JSON class in service:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

Service will be created in the selected path. Right click and activate the service.

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

Activate Service:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

Right click and test the service. Server URL will be generated.

Service URL:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

In SAP ECC, put a debugger in implemented HANDLE_REQUEST method of ZMG_TEST_JSON.

◉ Http Post in REST API:

POST is a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accept the data enclosed in the body of the request message. It is often used when uploading a file or when submitting a file in request body. Submitting the request with JSON body.

Open Postman and paste the generated URL. Check header parameters, paste JSON body and send the request.

Postman Collection:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

Send request to server for process:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

HTTP request methods (GET, POST, PUT, DELETE, COPY …) are embedded in header field with the name ‘~request_method’ uri service path of request header with the name ‘~request-uri’

◉ ABAP built for converting JSON data format to ABAP data format:

As per implemented method posted json will be parsed and will be stored in sap internal table.

HANDLE_REQUEST_Implementation:

METHOD if_http_extension~handle_request.
  TYPES: BEGIN OF ly_emp,
         number          TYPE char7,
         dateofjoining   TYPE char40,
         name            TYPE char40,
   END OF ly_emp.
  TYPES: BEGIN  OF ly_post_res,
         departmentcode   TYPE char10,
         departmentname   TYPE char50,
         employee         TYPE ly_emp,
         END OF ly_post_res.
  DATA:
        lv_request_uri      TYPE string,
        lv_request_method   TYPE string,
        lv_content_type     TYPE string,
        lv_req_body         TYPE string.
  DATA: cl_fdt_json   TYPE REF TO cl_fdt_json,
        lx_root       TYPE REF TO cx_root,
        ls_data       TYPE zmg_json_struct,
        lt_final      TYPE TABLE OF zmg_json_struct.

  lv_request_uri         = server->request->get_header_field( name = '~request_uri' ).
  lv_request_method      = server->request->get_header_field( name = '~request_method' ).
  lv_content_type        = server->request->get_header_field( name = 'content-type' ).

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

  IF lv_req_body IS NOT INITIAL.
    REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]' IN lv_req_body WITH space.
    REPLACE ALL OCCURRENCES OF REGEX '#' IN lv_req_body WITH space.
    CONDENSE lv_req_body.
    CREATE OBJECT cl_fdt_json.
    TRY.
        CALL METHOD cl_fdt_json=>json_to_data
          EXPORTING
            iv_json = lv_req_body
          CHANGING
            ca_data = ls_data.
      CATCH cx_root INTO lx_root.
    ENDTRY.
  ENDIF.

ENDMETHOD.

JSON data to ABAP data:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

Parsing JSON header data:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

Parsing JSON Item data:

ABAP REST API, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Jobs, SAP ABAP Skills

By following above steps, we can covert json data to ABAP data and store it into table.

No comments:

Post a Comment