Skip to Content
Author's profile photo Sean Li

How to use ICF to build service in GUI

ICF – Internet Communication Framwork

Internet Communication Framework (ICF) is a programming interface (API), based on interfaces and classes, used by ABAP programs to communicate with the Internet. ICF supports the Internet protocols HTTP, HTTPS, and SMTP. All communication between ABAP and the Internet based on these protocols takes place using ICF. ICF is a simplified technical foundation that can be used by more advanced programming interfaces. This means that ABAP programs do not usually access ICF directly and wrappers are used instead, such as Business Server Pages, Web Dynpro ABAP, Web Services ABAP, or OData-based services. Wrappers like these ensure that the conventions required for external communication are met, such as the model view controller approach (MVC) in Web Dynpro or Simple Object Access Protocol (SOAP) in Web services.

For more detail infomation, please scan https://help.sap.com/http.svc/rc/abapdocu_750_index_htm/7.50/en-US/abenicf.htm#@@ITOC@@ABENICF_1 .

 

Use ICF to Build Restful Service

Build ICF service

  1. User Tcode: sicf to define services.

You can choose one node to create your service, like zsean_srv. Then in the detail page, you            should input a description and click the Logon Data tab.

In the picture, there are some choices for Procedure: Standard means that you will use the                 standard Logon way to logon your service; Alternative Logon Procedure means that you can               choose one or more procedures for you service like below:                                                                    the third one means that you                need to assign the logon data the your service; and the last one means that you can use SSL            to your service.

If you don’t use SSL, please choose Standard in Security Requirement.

Choose the right user for Authentication, if your user can’t logon SAP backend, please choose            Internet User.(if not, logon error will always occurred when you access your service)

Fill your handler Class in Handler like below:                                                                                          

2. Implement handler class

You need to create a handler class extend interface:IF_HTTP_EXTENSION. In your class,                 please redifine the <IF_HTTP_EXTENSION>~HANDLE_REQUEST method. In this                             method, parameter server is runtime instance and you can get the request from                                   server->request and send the response with   server->response.

Define your handler class to handle restful request

Because ABAP doesn’t have JSON, so we need to serializes the json string from request to Internal Table. Tow class will help you to do it: a.  cl_trex_json_deserializer & cl_trex_json_serializer b. /UI2/CL_JSON; a have many limitation, such as an internal table:

user_name li si
user_id 1001

if you use the a to serialize to JSON, you will get “{user_name:”li si”, user_id:”1001″}” which can’t be json parse to json in JS. But you will get “{“userName”:”li si”, “userId”:”1001″}” which we best want with b.

 types: BEGIN OF ts,
   user_id    TYPE string,
   user_name  TYPE string,
   user_address TYPE string,
   END OF ts.

DATA ls_user TYPE ts.
DATA lt_user TYPE STANDARD TABLE OF  ts.

DATA lv_json TYPE string.
DATA lv_json1 TYPE string.

ls_user-user_id = '012145'.
ls_user-user_name = 'li si'.
ls_user-user_address =  'sicuan'.
append ls_user to lt_user.

ls_user-user_id = '0121456'.
ls_user-user_name = 'li wu'.
ls_user-user_address =  'sicuant'.
append ls_user to lt_user.

/ui2/cl_json=>serialize(
  EXPORTING
    data             = ls_user
*    compress         =
*    name             =
    pretty_name      = /ui2/cl_json=>pretty_mode-camel_case
*    type_descr       =
*    assoc_arrays     =
*    ts_as_iso8601    =
*    expand_includes  =
*    assoc_arrays_opt =
*    numc_as_string   =
  RECEIVING
    r_json           = lv_json
).

/ui2/cl_json=>serialize(
  EXPORTING
    data             = ls_user
*    compress         =
*    name             =
    pretty_name      = /ui2/cl_json=>pretty_mode-camel_case
*    type_descr       =
*    assoc_arrays     =
*    ts_as_iso8601    =
*    expand_includes  =
*    assoc_arrays_opt =
*    numc_as_string   =
  RECEIVING
    r_json           = lv_json1
).

write: lv_json  ,lv_json1.

the result of sample codes is:

 

And for GET query parameters you can use CL_HTTP_UTILITY to format. For example, an url like …/srv?langu=en&p1=2, you can get parameters via:

data(lv_value=server->request->get_header_field(if_http_header_fields_sap=>query_string).

the lv_calue should be “langu=en&p1=2”;

data(lt_query_fieldscl_http_utility=>string_to_fieldsstring lv_value ).

after formatted, the lt_query_fields should be

langu en
p1 2

 

How to handle File upload

we always use form to upload files, so how to handle?

If you upload files via form, in icf, you can get the file info form  “multiparts”.

sample codes:

  data: ls_file  type if_ap_ui_file_upload_handler=>ty_file,
        lt_file  type if_ap_ui_file_upload_handler=>tt_file,
        lv_text  type string,
        lt_form TYPE TIHTTPNVP,
        lv_id    type string.

* get header fields
  data(lv_value) = server->request->get_header_field( if_http_header_fields_sap=>query_string ).
  data(lt_query_fields) = cl_http_utility=>string_to_fields( string = lv_value ).

* Number of multipart request
  data(lv_num) = server->request->num_multiparts( ).

* get the file from the request
  data(lv_i) = 1.
  while lv_i <= lv_num.
    "get the file's request
    data(lo_request)  = server->request->get_multipart( lv_i ).
    "get file's name
    data(lv_filename) = lo_request->get_header_field( if_http_header_fields_sap=>content_filename ).
    if lv_filename is not initial.
      ls_file-file_name = lv_filename.
      "get file's mime type
      ls_file-content_type = lo_request->get_header_field( 'Content-Type' ). "#EC NOTEXT
      "get file's binary string
      ls_file-content      = lo_request->get_data( ).
      append ls_file to lt_file.
    endif.
    lv_i = lv_i + 1.
  endwhile.
  
  "get other form fields without file
  server->request->get_form_fields(
    EXPORTING
*      formfield_encoding = 0    " CO_FORMFIELD_ENCODING_RAW / _ENCODED
      source             = 2
    CHANGING
      fields             =   lt_form  " Form fields
  ).

from the sample codes, youc can get the file detail info and to do what you need to .

 

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.