Exposing service / REST APIs from SAP

We will be developing an API which will take CARRID as input in JSON payload and it will return relevant details from SFLIGHT table.

Goto TCode SICF → Below scree will appear

Execute → Navigate to default_host → sap → bc → Right click on bc → Click on New Sub-Element

Below popup will appear → Click OK

Enter the Service name → Click on OK

Provide values in below screen in Service Data Tab

Navigate to Logon Data Tab

Navigate to Handler List Tab → Enter the Service Handler Class name (ZCL_HTTPHANDLER)

Click on Save → Below message will appear → as we have not created the handler class yet.

Goto TCode SE24 to create the Class: ZCL_HTTPHANDLER

Navigate to Interface Tab → Enter Interface name: IF_HTTP_EXTENSION

Navigate to Attributes Tab → Add Attribute _HTTP_SERVER

Navigate to Method Tab → Method: IF_HTTP_EXTENSION~HANDLE_REQUEST will appear → Double click on Method name → Save → Activate.

Now move back to TCode SICF and Save the service → Now it will allow to save as Class is created and activated.

Also, we need to activate the Service → Right click on Service → Activate → Save

We have to write codes inside Method: IF_HTTP_EXTENSION~HANDLE_REQUEST to read the payload and return the response.

Code reference:

  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.


    _http_server  = server.
* Parse the Payload in SAP Structure/Table
        _request_data = server->request->get_cdata( ).

        CALL METHOD /ui2/cl_json=>deserialize
            json        = _request_data
            pretty_name = /ui2/cl_json=>pretty_mode-camel_case
            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>.
* Populate Error Table - Empty Payload
          _t_error = VALUE #( BASE _t_error ( type = 'E' message = 'Empty Payload' ) ).
          ASSIGN _t_error TO <fs_outtab>.

* 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_ ).
          o_response_->set_status( code = '500' reason = 'Internal Server Error' ).
      CATCH cx_root INTO _o_exception.
        _http_server->response->set_status( code = 500 reason = 'Internal Server Error' ).

Save and activate the Method/Class

It can be tested through Postman. Check this blog for API testing through Postman.

If you are not sure for the URL, check with your Basis Team

If Sflight Table is empty then Execute Program: SAPBC_DATA_GENERATOR to generate data.

For Empty Payload:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s