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:)
Trying to implement something similar, your step 4 and step 5 is little confusing. Could you ellaborate how you implemented standard interface methods GET with custom logic showing in screen shot to read driver details from table ?
Nice blog Rajat and very useful when Gateway is not an option.
Do you know how to handle the authetication of this restful api?
I tested the api successfully in "Test Service". But face the 401 issue when call this api in postman.
not sure if you've solved this yet.
To solve 401 issue, you must go to 'Authorization' tab ( which is beside 'Params' tab, choose the authentication type ( most commonly used is 'Basic Auth' ), provide the username and password which exist in the SAP system client you are testing at.
it's very nice blog.
What not simply use oData?
Is it possible to create multiple endpoints with handler?
You can define different endpoints in your root handler method using the router - attach method. You can either attach different provider class or handle the processing within a single provider class as you need.
Very informative blog.
Good ways to accept incoming REST API content aside from Odata services.