This is part of the series in “Bring Data to Life”. In this part we explore how we create a working RESTFul Web Service in SAP and handle the request in ABAP.
I have divided this tutorial in 3 parts:
Introduction to RESTful WS
RESTful WS is service implemented using HTTP using REST principles. In RESTful WS, you pass arguments in the URI itself like http://something.com/PARAM1. You extract this parameters or set of parameters (PARAM1/SUBparam1/Text1) and perform desired operation – GET, POST, PUT, DELETE. In GET, you get the data and send back the response. Using POST action, you try to POST the data within the system where RESTful WS is implemented.
Read more on RESTful WS on WIKI.
Step by step guide on Creating RESTful web service
1. Create RESTful WS in SAP
You can create the service in transaction SICF. Create a new service underneath the node default_host/sap/bc. You may want to create a new node, whenever you are creating a new RESTful WS as you can create different service underneath that node. Don’t use the node default_host/sap/bc/srt/ as it would required to have SOAMANAGER Configuration, which we are not going to have for our service.
Place cursor on desired node and select New Sub-Element. Enter the name of the Service in next Popup. In subsequent screen, enter the description.
2 Enter the Logon Data.
Select the tab Logon data and enter the required User credentials
3 Enter the Request Handler
In the handler tab, assign a class which would be used to handle the http request coming from the web. This class need to implement the interface IF_HTTP_EXTENSION in order to gain access of the request and also the required method. Press F1 to know more about the handler class.
Add the interface IF_HTTP_EXTENSION in your handler class Implement the method and activate the class.
Add the class in the Handler tab of the Service definition. You can create as many as handler class and assign them in the handler tab. All the classes here would be accessed in the sequence.
Locate your service in the service tree and activate it.
Web Service Testing
Once the class is active, Put an external break point in the method implementation. Locate your service and select Test Service from context menu. System will stop at your break-point.
You can note down the URI or URL and call the Service directly. Make sure you remove the client from the URL.
For now we would add this code in our handler class to interpret the request and send our response back. This would send response in HTML with text Hello Zevolving from Restful Ws.
At high level, code does this:
- Extract the Parameters from URI
- Do the logic
- Build the response HTML
Hello World from RESTFul WS
DATA: lv_path TYPE string, lv_cdata TYPE string, lv_param TYPE string. DATA: lt_request TYPE STANDARD TABLE OF string. * get the request attributes lv_path = server->request->get_header_field( name = '~path_info' ). SHIFT lv_path LEFT BY 1 PLACES. SPLIT lv_path AT '/' INTO TABLE lt_request. * build the response CONCATENATE '<head>' '' '</head>' '<body>' ` <h1>Hello ` lv_param ` from Restful WS</h1> ` '</body>' '</html>' INTO lv_cdata. * Send the response back server->response->set_cdata( data = lv_cdata ).
When I run it with the URL/sap/bc/zdemos/zd3_demo/zevolving, this is the output being generated from the Request handler class.
On to next – Putting all these together to make Fully working solution using D3.js
Also published at: Bring Data to Life – Integrating D3.js in SAP via RESTful Web Service on SCN.