Developing Plain RESTful APIs in ABAP — An Alternative to OData

Chairat Onyaem (Par)
4 min readSep 9, 2017

--

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.

Create operation suggests Read oepration

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?!

With function import, no body is accepted — only query parameters

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.

Select Independent Service for the endpoint you want to bind with your handler
Put in your handler class in the Handler List

Don’t forget to activate the node.

Test Your Endpoint

Select Test Service

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?

Complex Input/Output

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.

Data Model class — like POJO in Java

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.

Send a GET request with X-CSRF-Token=Fetch

Now, you can POST to your resource with the token. Let’s do with a blank request body and you will get 400 back.

Get 400 Bad Request when posting without request body

Test again with a valid request data.

You should get the proper response back with a valid request data

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (6)

Write a response