How to publish REST Web Service in SAP

Published on September 25, 2016 by Daniel Lanza

 sap

This tutorial will explain how to publish REST Web Service when SAP Netweaver Gateway is not available.

Exercise

Go to http://hostname:port/zrest?myVar=50 and the server will respond with the following content:

{"message": You have entered myVar=50 as query parameter."}

Be in mind that this content could be JSON or XML format. In this exercise, it is a basic JSON object.

Create custom class

Firstly, create a class object with IF_HTTP_EXTENSION interface.

Create custom class with IF_HTTP_EXTENSION interface
Create custom class with IF_HTTP_EXTENSION interface

Implement the method IF_HTTP_EXTENSION~HANDLE_REQUEST, where you will have the server parameter. There, the query parameter will be read from the request and the response will be set in JSON format.

method IF_HTTP_EXTENSION~HANDLE_REQUEST.

  DATA: lt_fields             TYPE tihttpnvp,
        lv_header_query       TYPE string,
        lv_html               TYPE string.

  FIELD-SYMBOLS: <fs_field>       LIKE LINE OF lt_fields.

*" get HEADER fields
  server->request->get_header_fields(
      CHANGING
        fields = lt_fields    " Header fields
    ).

  " Read the fields table and look for name "~query_string" -- this will contain the URL query
  READ TABLE lt_fields
    WITH KEY name = '~query_string'
    ASSIGNING <fs_field>.
  IF sy-subrc EQ 0.
    CONCATENATE '{"message": "You have entered'
                <fs_field>-value
                'as query parameter."}'
           INTO lv_html SEPARATED BY space.

*" Output to HTML
    server->response->set_cdata(
      EXPORTING
        data   = lv_html    " Character data
*        offset = 0    " Offset into character data
*        length = -1    " Length of character data
    ).
  ENDIF.

endmethod.

Create service in SICF

We need to expose the handler as a service, so go to SICF transaction, and add a new service in default_host.

Create a new service in SICF
Create a new service in SICF

In this service, a communication user is needed. It is recommended that the user is a service user instead of a dialog user.

If a user is not specified, authentication dialog will request when performing the REST Web Service request.

Set communication user for service
Set communication user for service

And finally, add the custom class as handler.

Set handler for service
Set handler for service

Test your service

A new service with ZREST name was created in default_host, so the URL to test is the following:

Firstly, right click on the service and select Test service. Your default browser will be launched with your SAP Client.

Test service from SICF
Test service from SICF

Change the query parameter and type anything. The response will be updated.

Test service with other query parameter
Test service with other query parameter
comments powered by Disqus

See also

How to fill flight model in SAP Netweaver ABAP Trial 7.03 SP04

This tutorial will explain how to fill flight model in SAP Netweaver. Flight model Table Description Keys T000 Client client SCURX Currencies currency SBUSPART Business partner client, partner number STRAVELAG Travel agencies client, travel agency number SCUSTOM Customers client, customer number SCARR Carriers client, carrier ID SCOUNTER Sales counters client, carrier ID, sales counter number SPFLI Flight schedule client, carrier ID, connection number SFLIGHT Flights client, carrier ID, connection number, date of flight SBOOK Flight bookings client, carrier ID, connection number, date of flight, booking number, customer number Run the following report in SE38 transaction:

Read more

How to enable SOAMANAGER in SAP Netweaver ABAP Trial 7.03 SP04

This tutorial will explain how to enable SOAMANAGER in SAP Netweaver ABAP Trial 7.03. Requirements A virtual machine with: Windows 7 and SAP Netweaver ABAP Trial Web GUI for SAP Netweaver enabled Issue When accessing SOAMANAGER, error code 403 turns up. Error code 403 SOAMANAGER If we get error code 500, check Web GUI for Netweaver works properly. It is explained in the installation post Solution Go to SICF transaction, and activate the following service:

Read more

How to install SAP Netweaver ABAP Trial 7.03 SP04 on Windows 7

This tutorial will explain how to install SAP Netweaver ABAP Trial 7.03 in a virtual machine. Requirements A virtual machine with: Windows 7 At least 4 GB RAM 80 GB hard disk space temporary during installation - 36 GB permanent. The faster the better Other things: Java JRE 1.5 installer A SCN account SAP Netweaver ABAP Trial 7.03 SP04 installer Steps Add Microsoft Loopback Adapter Install Java JRE Install SAP Netweaver Install a trial license key Enable Web GUI for SAP Netweaver Add Microsoft Loopback Adapter A network adapter with static IP address is needed.

Read more