Developing APIs in ABAP — just REST, not OData.
If you want to develop a simple RESTful API in ABAP, you might find a bit complicated dealing with OData limitations and NetWeaver Gateway configuration.
For example, if you want to develop a POST operation with OData, you need create an entity and assign some key fields. This may be your first issue if you don’t have an entity that you can define key for it. Second, OData does not seem allow you to have Create (POST) operation without Read (GET) operation.
Then you switch to OData Function Import. But the function import does not accept request body. You can only send parameter via query string. Hence, you can only send one flat structure. What if you want to send some table in?!
If you want to define our own resource and its operation without OData’s hassle then developing non-OData RESTful APIs is your friend.
Here is the instruction on how to create a simple API:
Create REST Handler and Resource Class
Start from create a REST Handler class by inheriting from CL_REST_HTTP_HANDLER. It will force you to redefine IF_REST_APPLICATION~GET_ROOT_HANDLER method.
This methods will handle incoming request at different resource paths you can define. Before we add a new path, let’s create a REST resource class first.
This class will handle incoming request. Let’s redefine IF_REST_RESOURCE~GET handler method and say hello.
Now get back to GET_ROOT_HANDLER method and define our path to our resource.
This tells the router to execute methods in class ZCL_REST_RESOURCE for incoming request at /hello resouce path.
Create ICF Node
To allow incoming request at specific endpoint, we need to create an ICF node in transaction SICF.
Don’t forget to activate the node.
Test Your Endpoint
It will open a browser window. Input your SAP login and append your resource path in the url and then you should see the response. Easy, isn’t it?
Now, let’s do something funnier. We will pass in data and return complex one with a POST method.
Create a Data Model Class
Let’s create a class to hold the data to response. It could be a simple structure. But I want a deep structure here so I have to create with a class.
Implement the method
Redefine the POST method in the resource class:
Note: Instead of using serialize/deserialize, you can use transformation in transaction XSLT_TOOL to convert back and forth which support both XML and JSON.
Test Your API with Postman
To test your API from external application, you need to get a token first.
Now, you can POST to your resource with the token. Let’s do with a blank request body and you will get 400 back.
Test again with a valid request data.
Note: Here I tested on my SAP Developer Edition instance setup on Azure. You can get one too. See instructions here.
If you want to have fully-functional SAP platform to try out ABAP, BW, HANA DB, SAPUI5, etc., this guide will show you…medium.com
Source Codes and Postman Scripts
Can be found here.