Creation of Rest API (Get & Post Method Call)
A REST API is Representational State Transfer Application Programming Interface that conforms to the constraints of REST architectural style and allows for interaction with RESTful services.
The most common methods are: GET, POST, PUT, and DELETE,
These methods would be used, a GET request to retrieve a record, a POST request to create one, a PUT request to update a record, and a DELETE request to delete one
Scenario –> Need to provide Driver detail based on the Driver ID.
Step 1 –>
Driver data base table.
Step 2 –>
Create Request Handler class ‘ZCL_DRIVER_REQ_HANDLER’ and inherit with standard class ‘CL_RESTHTTP_HANDLER’
Note –> It is mandatory to implement GET_ROOT_HANDLER method otherwise it will give syntax error.
Step 3 –>
Create Request Provider class ‘ZCL_DRIVER_REQ_PROVIDER’ and inherit with standard class ‘CL_REST_RESOURCE’
Step 4 –> Now Implement IF_REST_RESOURCE~GET method for reading the data.
After reading the data call /UI2/CL_JSON=>SERIALIZE() method to convert ABAP structure into JSON format.
Step 5 –> Implement GET_ROOT_HANDLER method of Request Handler class.
Here we need to link Request Handler class and Request Provider class with help of router.
Step 6 –> Create Service Element, TCode SICF
Step 7 –> Link Handler list, here we need to define our Request Handler class ‘ZCL_DRIVER_REQ_HANDLER’.
Step 8 –> Activate Service.
Step 9 –> Test Service.
Result –> Here we are passing Driver ID = 002 and based on ID data is displayed in JSON format.
Post Method call –>
Scenario –> We need to create new Driver detail in the data base. As you can see Driver ID ‘002’ is only exist in the system and now new ID ‘001’ should be created.
In variable LV_DATA, data is coming in JSON string
Call /UI2/CL_JSON=>DESERIALIZE() method to convert JSON string into ABAP structure.
Here is the new entry with ID ‘001’
After reading this blog you will be able to create simple Rest API and you will also have a idea of reading parameters in Request Provider class.
Please feel free to suggest if any correction is needed:)