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

      10 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 Siswanto Tan
      Siswanto Tan

      Hello Clark,

       

      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.

      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.

       

      Author's profile photo Hakan Atac
      Hakan Atac

      Is it possible to create multiple endpoints with handler?

      Author's profile photo Wispmac Shah
      Wispmac Shah

      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.

      Author's profile photo Siswanto Tan
      Siswanto Tan

      Very informative blog.

      Good ways to accept incoming REST API content aside from Odata services.

      Author's profile photo abhinav gupta
      abhinav gupta

      Thanks for sharing.

      From an operational support perspective. What are the options available to monitor the calls at the SAP end?

      For example in the case of proxy web services SAP has a utility tool like SXMB_MONI is there something similar available for the cases implemented by the above procedure?

       

      Regards,

      Abhinav