Skip to Content
Technical Articles
Author's profile photo Mithu Ghosh

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.

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.

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:

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

Activate Service:

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

Service URL:

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:

Send request to server for process:

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:

Parsing JSON header data:

Parsing JSON Item data:

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

  • Summary:

During one of our projects requirements, I have gone through several REST API documents. Most of the documents, blog post and articles describes the ways of REST API call which convert ABAP data to JSON data, not vice versa. From my learning and SAP-Non-SAP integration experience I have organized REST API call from JSON format to ABAP format without having SAP_GWFND (SAP Gateway Foundation) component or Separate Gateway server installed in landscape.

Please do like the blog post, if you find the content helpful. Also, do share your comments and inputs, if any.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Note that there are also many other blog posts about how to provide a Web Service as an ICF service, by searching with these keywords:

      "if_http_extension" site:blogs.sap.com

       

      Author's profile photo Mithu Ghosh
      Mithu Ghosh
      Blog Post Author

      Hi Sandra,

      SICF service you can find. As per my analysis this is the easiest approach to convert JSON data to ABAP data.

      Thanks ,
      Mithu

      Author's profile photo Jose Gregorio Vasquez Medina
      Jose Gregorio Vasquez Medina

      Excellent blog and very useful when there is no PO

      Author's profile photo Shai Sinai
      Shai Sinai

      Check out also /UI2/CL_JSON

      One more ABAP to JSON Serializer and Deserializer

      Author's profile photo Mithu Ghosh
      Mithu Ghosh
      Blog Post Author

      Our ECC version is 6.0 . Don't have /UI2/CL_JSON class available. To expose API for Azure this was the only option.

      Author's profile photo Shai Sinai
      Shai Sinai

      You may download the code from the wiki page and create your own Z class.