EXPOSE SERVICE / API FROM SAP – XML response

Please check this blog for SICF configuration and Service handler class creation.

In this example, passing the IDoc number through Params instead if Payload.

Payload is empty.

  METHOD if_http_extension~handle_request.

    DATA : _o_idoc     TYPE REF TO cl_idoc_xml1,
           _str        TYPE string,
           o_response_ TYPE REF TO if_http_response,
           _idocnum    TYPE edi_docnum.

* Get the Parameters
    DATA(_parameters)   = server->request->get_header_field( if_http_header_fields_sap=>query_string ).
    DATA(_http_server)  = server.

    SPLIT _parameters AT '=' INTO DATA(_field) _idocnum.
    _idocnum = |{ _idocnum ALPHA = IN }|.

    IF _field EQ 'idoc' AND _idocnum IS NOT INITIAL.
* If input IDoc exists !
      SELECT SINGLE docnum, status FROM edidc
        INTO @DATA(_s_edidc) WHERE docnum EQ @_idocnum.
      IF sy-subrc IS INITIAL.
* IDoc XML
        TRY.
            CREATE OBJECT _o_idoc
              EXPORTING
                docnum             = _idocnum
              EXCEPTIONS
                error_loading_idoc = 1
                error_building_xml = 2
                OTHERS             = 3.
            IF sy-subrc IS INITIAL.
              CALL METHOD _o_idoc->get_xmldata_as_string
                IMPORTING
                  data_string = _str.

              REPLACE : ALL OCCURRENCES OF '<![CDATA[' IN _str WITH '',
                        ALL OCCURRENCES OF ']]>' IN _str WITH ''.

              o_response_ = _http_server->response.
              o_response_->set_content_type( 'text/xml' ).
              o_response_->set_cdata( _str ).
            ENDIF.
          CATCH cx_root INTO DATA(_o_exception).
* Handle exception here
        ENDTRY.
      ENDIF.
    ENDIF.

  ENDMETHOD.

XML Response.

Here I took the example of IDoc, incase if you have to convert Table data to XML, then please use TCode XSLT_TOOL and use Transformation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s