Sie sind auf Seite 1von 7

Products

Products Industries
Industries Services and Support
Services and Support Training
Training Community
Community Developer
Developer Partner
Partner

About
About

 
Ask a Question Write a Blog Post Login

Diogo Awaihara
August 5, 2016 3 minute read

RESTfull webservice in ABAP returning JSON


Follow RSS feed Like

4 Likes 10,451 Views 3 Comments

It is very simple to create a webservice using only ABAP, thanks to Naresh Bammidi that showed us the way!

Basically, we start by creating a new class in SE24 implementing interface IF_HTTP_EXTENSION.

Then, we just implement the method handle_request.

The trick is to concatenate everything in a single string, using the JSON style, and set the content type by using the method
set_header_ eld.

Check my sample code bellow:


METHOD if_http_extension~handle_request.

     CONSTANTS: c_object_start TYPE c VALUE ‘{‘,

                c_object_end   TYPE c VALUE ‘}’.

     DATA: lv_path_info      TYPE string,

           lv_request_method TYPE string,

           lv_response_data  TYPE string.

     DATA: it_inputparams TYPE tihttpnvp,

           ls_inputparams TYPE LINE OF tihttpnvp.

     DATA: lv_ebeln TYPE ekpo-ebeln,

           lv_ebelp TYPE ekpo-ebelp,

           lv_matnr TYPE ekpo-matnr.

     FIELD-SYMBOLS <fs_param> TYPE LINE OF tihttpnvp.

     “Get request info

     lv_path_info = server->request->get_header_ eld( name = ‘~path_info’ ).

     lv_request_method = server->request->get_header_ eld( name = ‘~request_method’ ).

     “Only GET is allowed

     IF lv_request_method NE ‘GET’.

       CALL METHOD server->response->set_header_ eld( name = ‘Allow’ value = ‘GET’ ).

       CALL METHOD server->response->set_status( code = ‘405’ reason = ‘Method not allowed’ ).

       EXIT.

     ENDIF.

     “Get GET parameters

     CALL METHOD server->request->get_form_ elds

       CHANGING
         elds = it_inputparams.

     UNASSIGN <fs_param>.

     LOOP AT it_inputparams ASSIGNING <fs_param>.

       TRANSLATE <fs_param>-name TO UPPER CASE.

     ENDLOOP.

     CLEAR: lv_ebeln, ls_inputparams.

     READ TABLE it_inputparams INTO ls_inputparams WITH KEY name = ‘PO’.

     lv_ebeln = ls_inputparams-value.

     CLEAR ls_inputparams.

     “Empty parameter

     IF lv_ebeln IS INITIAL.

       CALL METHOD server->response->set_status( code = ‘404’ reason = ‘Empty parameters are not allowed’ ).

       CALL METHOD server->response->set_cdata( data = ‘There are one or more missing parameters’ ).

       EXIT.

     ENDIF.

     “Start of response data

     lv_response_data = c_object_start.

     “Add element: PO

     lv_response_data = | { lv_response_data } “PO”:” { lv_ebeln }”, |.

     “Add array: ITEMS

     lv_response_data = | { lv_response_data } “ITEMS”:[ |.

     “Add items

     SELECT ebelp matnr

       INTO (lv_ebelp, lv_matnr)


       FROM ekpo

       WHERE ebeln = lv_ebeln.

       lv_response_data = | { lv_response_data } { c_object_start } “NITEM”:”{ lv_ebelp }”,”MATERIAL”:”{ lv_matnr }” {


c_object_end },|.

     ENDSELECT.

     IF sy-subrc = 0.

       SHIFT lv_response_data RIGHT DELETING TRAILING space.

     ENDIF.

     “End of array

     lv_response_data = | { lv_response_data } ] |.

     “End of response data

     lv_response_data = | { lv_response_data } { c_object_end } |.

     “Set JSON Content-Type

     CALL METHOD server->response->set_header_ eld( name = ‘Content-Type’ value = ‘application/json;


charset=UTF-8’ ).

     “Set Response Data

     CALL METHOD server->response->set_cdata( data = lv_response_data ).

ENDMETHOD.

After that just publish it in SICF, into Handler list and test it.
I tested it in SoapUI 5.2.1.

Alert Moderator

Assigned tags

ABAP Development | abap | json | rest | restfulwebservice |

View more...
Reference doc: http://scn.sap.com/docs/DOC-46463

Related Blog Posts

ABAP News for Release 7.40 – ABAP and JSON


By Horst Keller , Jul 04, 2013
ABAP and JSON
By Horst Keller , Jan 07, 2013
Parsing JSON in ABAP
By Kerem Koseoglu , Aug 03, 2017
Related Questions

JSON Deserialization in ABAP


By Mark André Kluck , Oct 18, 2018
Create JSON with date in Unix Time format
By Sebastian Rötzel , Jan 02, 2017
I need to convert a json string into an abap structure
By Rachid Moulakhnif , Nov 06, 2018

3 Comments

You must be Logged on to comment or reply to a post.

Horst Keller

August 6, 2016 at 11:37 am


See also the ICF chapter and the examples there

ABAP Keyword Documentation

Like (0)

Horst Keller

August 9, 2016 at 4:19 pm


PS: And don’t forget security (danger of Cross Side Scripting etc.). I forgot once, how embarrassing that was ..

Like (0)

Rüdiger Plantiko

August 8, 2016 at 7:19 am


It really is that easy.

And it is a great moment when you made it work the rst time and see how easy it actually is!

But the next steps would be

Make one service responsible for di erent types of requests – which leads to some kind of parsing the request and
delegating a work to a “worker class”.
Instead of producing JSON with strings, produce it the right way with transformations or with a builder class working on
base of IF_IXML_DOCUMENT. Find examples for the di erent ways to do it in the ABAP docu! It is easier, less error
prone, and the right level to work with “foreign” data formats like JSON or XML.
For each request, di erent resonse data types may occur. Separate the general HTTP communication, i.e. transforming
data to JSON and putting them into the response, from the speci c tasks of the handler which refer to concrete data in
the ABAP world and retrieve ABAP data to the general part.

I outlined some of these tasks in this blog from 2013. For some of the things described there, there are better ways in the
meantime. The main gap of that blog is that there even is a complete API framework for restful services nowadays.

Study the docu for more details on these issues.

Best regards

Rüdiger

Like (0)

Share & Follow

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Sitemap

Newsletter