Skip to Content
Technical Articles
Author's profile photo Rajat verma

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’

 

Conclusion –> 

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:)

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Drashti Amrutia
      Drashti Amrutia

      Hi Rajat

      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 ?

      many thanks

      Drashti

      Author's profile photo Drashti Amrutia
      Drashti Amrutia

      solved myself.

      Nice blog Rajat and very useful when Gateway is not an option.

      Author's profile photo Clark Huang
      Clark Huang

      HI Rajat,

      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.

      Author's profile photo ramana murthy
      ramana murthy

      Hi Rajat,

      it's very nice blog.

      Author's profile photo David Fryda
      David Fryda

      Hi Rakst,

      Thank you.

      What not simply use oData?

       

      Regards.