Exposing a RESTful Parameterized Service Using OO ABAP and SICF, Tested with Postman

Estimated read time 8 min read

In this blog, we will walk through the steps to expose a REST-based parameterized service in ABAP using SICF and a custom handler class. This will help in handling HTTP requests and returning appropriate responses.

Steps to Create the REST Service

Define the Service in SICF

Navigate to Transaction SICF.

Execute (F8) → Navigate to default_hostsapbc.

Right-click on bc → Click on New Sub-Element.In the popup window, click OK.Enter the service name and click OK.

In the Service Data tab, provide the necessary details.

Navigate to the Logon Data tab and configure authentication settings.

Navigate to the Handler List tab → Enter the service handler class name: ZCL_EXP_PARM_SRV.

Click Save. 

You may see a message indicating that the handler class does not exist yet. This will be created in the next step.

Create the Handler Class

Navigate to Transaction SE24.
Create a class named ZCL_EXP_PARM_SRV.

Navigate to the Interfaces tab → Enter the interface name: IF_HTTP_EXTENSION.

Navigate to the Attributes tab → Add the attribute _HTTP_SERVER.

Navigate to the Methods tab → Locate the method IF_HTTP_EXTENSION~HANDLE_REQUEST.

Double-click on the method name, then save and activate the class.

Activate the Service in SICF

Implement the Method IF_HTTP_EXTENSION~HANDLE_REQUEST

To process incoming HTTP requests, implement the method IF_HTTP_EXTENSION~HANDLE_REQUEST in your handler class. This method will:

Read the incoming payload.

Process parameters.

Return the appropriate response.

 

 

METHOD if_http_extension~handle_request.
” Define error structure
TYPES : BEGIN OF ty_error,
type TYPE char1,
message TYPE char50,
END OF ty_error.

” Define variables
DATA : respons_data_ TYPE string, ” Response data in JSON format
_t_error TYPE STANDARD TABLE OF ty_error, ” Error table
o_response_ TYPE REF TO if_http_response, ” HTTP Response object
_o_exception TYPE REF TO cx_root. ” Exception object

FIELD-SYMBOLS : <fs_outtab> TYPE ANY TABLE. ” Generic table field symbol

” Assign HTTP server instance
_http_server = server.

TRY.
” Parse the request payload
DATA(_request_data) = server->request->get_cdata( ).

” Get the HTTP method type (GET, POST, etc.)
DATA(_method) = server->request->get_header_field( if_http_header_fields_sap=>request_method ).

” Retrieve query string parameters
DATA(_parameters) = server->request->get_header_field( if_http_header_fields_sap=>query_string ).

” Check if parameters exist
IF _parameters IS NOT INITIAL.
” Extract the parameter name and value
SPLIT _parameters AT ‘=’ INTO DATA(_fieldname) DATA(_fieldval).

” Fetch data from SFLIGHT table based on parameter value
SELECT * FROM sflight INTO TABLE @DATA(_t_sflight)
WHERE carrid EQ @_fieldval.

” Assign the result to field symbol
ASSIGN _t_sflight TO <fs_outtab>.

” Handle case when no data is found
IF sy-subrc IS NOT INITIAL.
_t_error = VALUE #( BASE _t_error ( type = ‘E’ message = ‘No data found’ ) ).
ASSIGN _t_error TO <fs_outtab>.
ENDIF.
ELSE.
” Populate error message when parameters are missing
_t_error = VALUE #( BASE _t_error ( type = ‘E’ message = ‘Empty Parameters’ ) ).
ASSIGN _t_error TO <fs_outtab>.
ENDIF.

” Convert the output table to JSON format
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 ).

” Prepare HTTP response
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.
” Set response status to 500 when no data is available
o_response_->set_status( code = ‘500’ reason = ‘Internal Server Error’ ).
ENDIF.

” Handle exceptions
CATCH cx_root INTO _o_exception.
_http_server->response->set_status( code = 500 reason = ‘Internal Server Error’ ).
ENDTRY.
ENDMETHOD.

 

 

After implementing the logic, save and activate the method and the class.

Testing the Service

To test the service, use Postman or any other REST client.

If SFLIGHT Table is Empty

If the SFLIGHT table has no data, execute the program SAPBC_DATA_GENERATOR to generate sample data.

Instead of sending a payload, pass values through URL parameters. The request body should remain empty.

Output

Conclusion
By following these steps, you can effectively expose a REST parameterized service in ABAP. This approach enables seamless communication using HTTP requests and responses, facilitating integration with external systems. Additionally, it enhances interoperability and ensures efficient data exchange within SAP environments.

 

 

 

​ In this blog, we will walk through the steps to expose a REST-based parameterized service in ABAP using SICF and a custom handler class. This will help in handling HTTP requests and returning appropriate responses.Steps to Create the REST ServiceDefine the Service in SICFNavigate to Transaction SICF.Execute (F8) → Navigate to default_host → sap → bc.Right-click on bc → Click on New Sub-Element.In the popup window, click OK.Enter the service name and click OK. In the Service Data tab, provide the necessary details. Navigate to the Logon Data tab and configure authentication settings. Navigate to the Handler List tab → Enter the service handler class name: ZCL_EXP_PARM_SRV. Click Save. You may see a message indicating that the handler class does not exist yet. This will be created in the next step. Create the Handler ClassNavigate to Transaction SE24.Create a class named ZCL_EXP_PARM_SRV. Navigate to the Interfaces tab → Enter the interface name: IF_HTTP_EXTENSION. Navigate to the Attributes tab → Add the attribute _HTTP_SERVER.Navigate to the Methods tab → Locate the method IF_HTTP_EXTENSION~HANDLE_REQUEST.Double-click on the method name, then save and activate the class.Activate the Service in SICF Implement the Method IF_HTTP_EXTENSION~HANDLE_REQUESTTo process incoming HTTP requests, implement the method IF_HTTP_EXTENSION~HANDLE_REQUEST in your handler class. This method will:Read the incoming payload.Process parameters.Return the appropriate response.   METHOD if_http_extension~handle_request.
” Define error structure
TYPES : BEGIN OF ty_error,
type TYPE char1,
message TYPE char50,
END OF ty_error.

” Define variables
DATA : respons_data_ TYPE string, ” Response data in JSON format
_t_error TYPE STANDARD TABLE OF ty_error, ” Error table
o_response_ TYPE REF TO if_http_response, ” HTTP Response object
_o_exception TYPE REF TO cx_root. ” Exception object

FIELD-SYMBOLS : <fs_outtab> TYPE ANY TABLE. ” Generic table field symbol

” Assign HTTP server instance
_http_server = server.

TRY.
” Parse the request payload
DATA(_request_data) = server->request->get_cdata( ).

” Get the HTTP method type (GET, POST, etc.)
DATA(_method) = server->request->get_header_field( if_http_header_fields_sap=>request_method ).

” Retrieve query string parameters
DATA(_parameters) = server->request->get_header_field( if_http_header_fields_sap=>query_string ).

” Check if parameters exist
IF _parameters IS NOT INITIAL.
” Extract the parameter name and value
SPLIT _parameters AT ‘=’ INTO DATA(_fieldname) DATA(_fieldval).

” Fetch data from SFLIGHT table based on parameter value
SELECT * FROM sflight INTO TABLE @DATA(_t_sflight)
WHERE carrid EQ @_fieldval.

” Assign the result to field symbol
ASSIGN _t_sflight TO <fs_outtab>.

” Handle case when no data is found
IF sy-subrc IS NOT INITIAL.
_t_error = VALUE #( BASE _t_error ( type = ‘E’ message = ‘No data found’ ) ).
ASSIGN _t_error TO <fs_outtab>.
ENDIF.
ELSE.
” Populate error message when parameters are missing
_t_error = VALUE #( BASE _t_error ( type = ‘E’ message = ‘Empty Parameters’ ) ).
ASSIGN _t_error TO <fs_outtab>.
ENDIF.

” Convert the output table to JSON format
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 ).

” Prepare HTTP response
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.
” Set response status to 500 when no data is available
o_response_->set_status( code = ‘500’ reason = ‘Internal Server Error’ ).
ENDIF.

” Handle exceptions
CATCH cx_root INTO _o_exception.
_http_server->response->set_status( code = 500 reason = ‘Internal Server Error’ ).
ENDTRY.
ENDMETHOD.  After implementing the logic, save and activate the method and the class.Testing the ServiceTo test the service, use Postman or any other REST client.If SFLIGHT Table is EmptyIf the SFLIGHT table has no data, execute the program SAPBC_DATA_GENERATOR to generate sample data.Instead of sending a payload, pass values through URL parameters. The request body should remain empty. OutputConclusionBy following these steps, you can effectively expose a REST parameterized service in ABAP. This approach enables seamless communication using HTTP requests and responses, facilitating integration with external systems. Additionally, it enhances interoperability and ensures efficient data exchange within SAP environments.     Read More Application Development and Automation Blog Posts articles 

#SAP

You May Also Like

More From Author