create rest based service (web service) in sap abap

Step by step process for creating a rest api (web service ) in sap, which will be consume by web based application :

 Project Requirement : 

1.      Create a rest based service, which will be called by customer portal or mobile app for creating the customer complaint.

2.      Generate unique id for each complaint and send the created id in response.

3.      If complaint id pass through portal, send the details status of the complaint. 

Data will be pass from portal:

1.      Customer Name

2.      Mobile Number

3.      Email ID

4.      Complaint description

In response sent the complaint number

Development approach:

1.      Create a table.

2.      Create SNRO Number range object for complaint id.

3.      Create request handler class   - ZCL_WEBCOMPLAINT_RH

4.      Create resource class and implement get and post method.

5.      Create SICF service and activate

6.      Test the complete cycle with postman.

1: Create Table

Transaction Code -SE11  - ZREST_COMPLAINT


2: Create SNRO Number range object : ZRESTC

    Transaction code : SNRO

give the object id as ZRESTC and click on create


enter short text, long text, number length domain, % warning and save.


click on Ranges 


click on change intervals 

click on insert line

  insert the, number range and save. 

3: Create request handler class : 

    Transaction Code - SE24 

    give the class id ZCL_WEBCOMPLAINT_RH and click on create 

give the short description and click on create inheritance 

enter the superclass as cl_rest_http_handler and click on save.

select the get_root_handler and click on redefine

add the below code in between method and endmethod

     data(lo_routernew cl_rest_router).

         IV_TEMPLATE      =  '/webcomplaint'    " Unified Name for Resources
         IV_HANDLER_CLASS =  'ZCL_WEBCOMPLAINT_RP'   " Object Type Name
*         IT_PARAMETER     =     " Resource contructor parameters

         RO_ROOT_HANDLER lo_router.

save and activate the class

4: Create resource provider class : Transaction SE24


enter the class id and click on create.

enter description, click on create inheritance cl_rest_resource and click on save.

select the method 'GET' and click on redefine .

add the below code

*    .

    data lv_string1 type vbeln,         "string,
           lv_string2 type string,
           gs_complaint type ZREST_COMPLAINT.

    lv_string1 mo_request->GET_URI_QUERY_PARAMETERiv_name 'ID' ).

        INPUT         lv_string1
       OUTPUT        lv_string1.

    select SINGLE from zrest_complaint into CORRESPONDING FIELDS OF GS_COMPLAINT
    WHERE id lv_string1.

       DATA        =  gs_complaint    " Data to serialize
*       COMPRESS    = ABAP_FALSE    " Skip empty elements
*       NAME        =     " Object name
*       PRETTY_NAME =     " Pretty Print property names
*       TYPE_DESCR  =     " Data descriptor
       R_JSON      =   lv_string2  " JSON string


        IV_NAME  =  'Content-Type'   " Header Name
        IV_VALUE =  'application/json'   " Header Value



save and activate .

similar redefine the post method and add the below code

*    IO_ENTITY =
*    .

    data lv_string1 type vbeln,         "string,
           lv_string2 type string,
           lv_response type string,
           gs_complaint type ZREST_COMPLAINT.

    data(lo_entity)    mo_request->GET_ENTITY).
    data(lo_response)  mo_response->CREATE_ENTITY).

    "read string data i.e json

     JSON                  =  lv_data   " JSON string
*     PRETTY_NAME           =     " Pretty Print property names
     DATA                  =  gs_complaint   " Data to serialize

        NR_RANGE_NR                   '01'
        OBJECT                        'ZRESTC'
       QUANTITY                      '1'
*       SUBOBJECT                     = ' '
*       TOYEAR                        = '0000'
*       IGNORE_BUFFER                 = ' '
       NUMBER                        GS_COMPLAINT-id
*       QUANTITY                      =
*       RETURNCODE                    =
       INTERVAL_NOT_FOUND            1
       OBJECT_NOT_FOUND              3
       QUANTITY_IS_0                 4
       QUANTITY_IS_NOT_1             5
       INTERVAL_OVERFLOW             6
       BUFFER_OVERFLOW               7
       OTHERS                        8
    IF SY-SUBRC <> 0.
* Implement suitable error handling here

    GS_COMPLAINT-createdby    sy-uname.
    GS_COMPLAINT-time         sy-uzeit.


        DATA        =   gs_complaint    " Data to serialize
*        COMPRESS    = ABAP_FALSE    " Skip empty elements
*        NAME        =     " Object name
*        PRETTY_NAME =     " Pretty Print property names
*        TYPE_DESCR  =     " Data descriptor
        R_JSON      =    LV_RESPONSE   " JSON string



save and activate the class.


5: Create SICF service : Transaction SICF

click on execute.

select default_host and click on create host/ service

give the service id and press enter.

enter description and handler class and save. zcl_webcomplaint_rh

on sicf main screen activate the service.

now before testing add one record in our table , then test the service.

do the testing as per process given in the demo videos.

part 1 of 2

part 2 of 2

Thank you.....

Kindly share your feedback ...

Post a Comment


  1. Thanx brother for your valuable video, can you make an demo on how to call this web service from outside SAP like VB, VB6, Android etc.?

    Thanks in advance... best regards

  2. another request, please make an demo with post request from SAP/VB6 etc.

  3. Very nicely you explained and demonstrated

  4. Nice post. Can you expand on the security and credentials side of this. Automated API's won't be logging in manually. How do you pass credentials securely in the api calls?


Total Pageviews