EXPOSE Parameterized SERVICE / APIS FROM SAP

For detail SICF configuration please check this Blog.

Instead of Payload, we will put the values in Parameters, Payload will be empty.

Below code will populate the parameters, string after ? (carrid=AA) in current case into variable _parameters. Once we have the parameter string, we can validate and proceed further. 

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

Code Reference:

  METHOD if_http_extension~handle_request.

    TYPES : BEGIN OF ty_error,
              type    TYPE char1,
              message TYPE char50,
            END OF ty_error.

    DATA : respons_data_ TYPE string,
           _t_error      TYPE STANDARD TABLE OF ty_error,
           o_response_   TYPE REF TO if_http_response,
           _o_exception  TYPE REF TO cx_root.

    FIELD-SYMBOLS : <fs_outtab> TYPE ANY TABLE.


    _http_server  = server.
    TRY.
* Parse the Payload in SAP Structure/Table
        DATA(_request_data) = server->request->get_cdata( ).
* Request method
        DATA(_method)       = server->request->get_header_field( if_http_header_fields_sap=>request_method ).
* Get the Parameters
        DATA(_parameters)   = server->request->get_header_field( if_http_header_fields_sap=>query_string ).

        IF _parameters IS NOT INITIAL.

* Get the Parameter name & value - You can write validations if parameter name is different
          SPLIT _parameters AT '=' INTO DATA(_fieldname) DATA(_fieldval).

* Data Extraction
          SELECT * FROM sflight INTO TABLE @DATA(_t_sflight)
            WHERE carrid EQ @_fieldval.
          ASSIGN _t_sflight TO <fs_outtab>.
          IF sy-subrc IS NOT INITIAL.
* Populate Error Table - for No data found
            _t_error = VALUE #( BASE _t_error ( type = 'E' message = 'No data found' ) ).
            ASSIGN _t_error TO <fs_outtab>.
          ENDIF.
        ELSE.
* Populate Error Table - Empty Payload
          _t_error = VALUE #( BASE _t_error ( type = 'E' message = 'Empty Parameters' ) ).
          ASSIGN _t_error TO <fs_outtab>.
        ENDIF.

* Output table to Json
        IF <fs_outtab> IS ASSIGNED.
          respons_data_ = /ui2/cl_json=>serialize(
                              data        = <fs_outtab>
                              compress    = abap_false
                              pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).

          o_response_ = _http_server->response.

          o_response_->set_status( code = '200' reason = 'OK' ).
          o_response_->set_content_type( 'application/json' ).

          o_response_->set_cdata( respons_data_ ).
        ELSE.
          o_response_->set_status( code = '500' reason = 'Internal Server Error' ).
        ENDIF.
      CATCH cx_root INTO _o_exception.
        _http_server->response->set_status( code = 500 reason = 'Internal Server Error' ).
    ENDTRY.
  ENDMETHOD.

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