Developing Plain RESTful APIs in ABAP — An Alternative to OData

Chairat Onyaem
Sep 9, 2017 · 4 min read
Image for post
Image for post

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.

Image for post
Image for post
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?!

Image for post
Image for post
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.

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

Don’t forget to activate the node.

Test Your Endpoint

Image for post
Image for post
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.

Image for post
Image for post
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.

Image for post
Image for post
Get 400 Bad Request when posting without request body

Test again with a valid request data.

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

Personal blog of a developer who passionate in new…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store