Introduction
In abap, we create and expose API service through the OData gateway client for integration with third-party systems. This blog focuses on how to retrieve data from a REST API using a standard ABAP class. I have created the OData service using the GET_ENTITY method in an OData project and consumed that API using the approach described below.
cl_http_clientif_http_client****Data Declarations****
TYPES: BEGIN OF tt_json,
MaterialCode TYPE matnr, “Material
UserName TYPE ernam, “Creator Name
mtart TYPE mtart, “Material Type
END OF tt_json.
TYPES: BEGIN OF ty_field,
fielname TYPE char40, “Field Name
END OF ty_field.
DATA: lt_material TYPE TABLE OF tt_json,
lt_tab TYPE TABLE OF ty_field,
ls_material TYPE tt_json,
lo_http TYPE REF TO if_http_client, “HTTP Client Abstraction
lr_data TYPE REF TO data,
lv_result TYPE string,
lv_content_type TYPE string,
lv_action_type TYPE string.
FIELD-SYMBOLS: <ls_data> TYPE data,
<ls_d> TYPE data,
<ls_struct> TYPE data,
<lv_any> TYPE any,
<lv_char>.
SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE TEXT-002.
PARAMETERS: p_mat TYPE matnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK a1.
*Get URL
DATA(lv_url) = |{ TEXT-001 }'{ p_mat }’)| .
The API and selection screen look likes,
“To perform the HTTP communication using factory method.
cl_http_client=>create_by_url(
EXPORTING
url = lv_url ” URL
IMPORTING
client = lo_http ” HTTP Client Abstraction
EXCEPTIONS
argument_not_found = 1 ” Communication parameter (host or service) not available
plugin_not_active = 2 ” HTTP/HTTPS communication not available
internal_error = 3 ” Internal error (e.g. name too long)
OTHERS = 7
).
“
IF sy-subrc = 0.
lv_content_type = ‘application/json’.
lv_action_type = ‘GET’.
lo_http->request->set_content_type( lv_content_type ).
lo_http->request->set_method( lv_action_type ).
“The output shows a json format.
lo_http->request->set_header_field(
EXPORTING
name = ‘Accept’ ” Name of the header field
value = ‘application/json’ ” HTTP header field value
).
“To ignore the authentication during execution.
lo_http->propertytype_logon_popup = if_http_client=>co_disabled.
lo_http->authenticate(
EXPORTING
proxy_authentication = ‘ ‘ ” Proxy Logon (= ‘X’)
client = ‘800’ ” R/3 system (client number from logon)
username = ‘username’ ” ABAP System, User Logon Name
password = ‘password’ ” Logon ID
language = ‘E’ ” SAP System, Current Language
).
ENDIF.
lo_http->send(
EXPORTING
timeout = 15 ” Timeout of Answer Waiting Time
EXCEPTIONS
http_communication_failure = 1 ” Communication Error
http_invalid_state = 2 ” Invalid state
http_processing_failed = 3 ” Error when processing method
http_invalid_timeout = 4 ” Invalid Time Entry
OTHERS = 5
).
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
lo_http->receive(
EXCEPTIONS
http_communication_failure = 1 ” Communication Error
http_invalid_state = 2 ” Invalid state
http_processing_failed = 3 ” Error when processing method
OTHERS = 4
).
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*Get the response from target as character string cdata( ).
lv_result = lo_http->response->get_cdata( ).
lo_http->close( ).
We have the response from target system and need to retrieve the data from the string variable lv_result.
*Deserialize the data from Json to Abap
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_result ” JSON string
CHANGING
data = lr_data ” Data to serialize
).
lt_tab = VALUE #( ( fielname = ‘MATERIALCODE’)
( fielname = ‘USERNAME’)
( fielname = ‘MTART’ ) ).
ASSIGN lr_data->* TO <ls_data>.
ASSIGN COMPONENT ‘d’ OF STRUCTURE <ls_data> TO <ls_d>.
DATA(lo_typedescr) = cl_abap_typedescr=>describe_by_data( <ls_d> ).
ASSIGN <ls_d>->* TO <ls_struct>.
IF <ls_struct> IS ASSIGNED.
LOOP AT lt_tab INTO DATA(ls_tab).
ASSIGN COMPONENT ls_tab-fielname OF STRUCTURE <ls_struct> TO <lv_any>.
IF <lv_any> IS ASSIGNED.
ASSIGN <lv_any>->* TO <lv_char>.
IF <lv_char> IS ASSIGNED.
ASSIGN COMPONENT ls_tab-fielname OF STRUCTURE ls_material TO FIELD-SYMBOL(<lv_target>).
IF <lv_target> IS ASSIGNED.
<lv_target> = <lv_char>.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
UNASSIGN: <ls_data>,<ls_d>,<ls_struct>,<lv_any>,<lv_char>,<lv_target>.
cl_demo_output=>begin_section( ‘Material Detail’ ).
cl_demo_output=>write_data( ls_material ).
cl_demo_output=>next_section( ‘OData Service URL’ ).
cl_demo_output=>write_text( lv_url ).
cl_demo_output=>display( ).
CLEAR: ls_material.
The output should be like below in debugger level.
Test the above service URI to postman app in GET method.
Note: In the example above, I retrieved data from an OData HTTP API that does not require an SSL certificate. However, when communication with external APIs. an SSL certificate is typically required. We will cover this in detail in a separate blog post.
Thank you!
IntroductionIn abap, we create and expose API service through the OData gateway client for integration with third-party systems. This blog focuses on how to retrieve data from a REST API using a standard ABAP class. I have created the OData service using the GET_ENTITY method in an OData project and consumed that API using the approach described below.cl_http_clientif_http_client****Data Declarations****
TYPES: BEGIN OF tt_json,
MaterialCode TYPE matnr, “Material
UserName TYPE ernam, “Creator Name
mtart TYPE mtart, “Material Type
END OF tt_json.
TYPES: BEGIN OF ty_field,
fielname TYPE char40, “Field Name
END OF ty_field.
DATA: lt_material TYPE TABLE OF tt_json,
lt_tab TYPE TABLE OF ty_field,
ls_material TYPE tt_json,
lo_http TYPE REF TO if_http_client, “HTTP Client Abstraction
lr_data TYPE REF TO data,
lv_result TYPE string,
lv_content_type TYPE string,
lv_action_type TYPE string.
FIELD-SYMBOLS: <ls_data> TYPE data,
<ls_d> TYPE data,
<ls_struct> TYPE data,
<lv_any> TYPE any,
<lv_char>.
SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE TEXT-002.
PARAMETERS: p_mat TYPE matnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK a1.
*Get URL
DATA(lv_url) = |{ TEXT-001 }'{ p_mat }’)| .The API and selection screen look likes, “To perform the HTTP communication using factory method.
cl_http_client=>create_by_url(
EXPORTING
url = lv_url ” URL
IMPORTING
client = lo_http ” HTTP Client Abstraction
EXCEPTIONS
argument_not_found = 1 ” Communication parameter (host or service) not available
plugin_not_active = 2 ” HTTP/HTTPS communication not available
internal_error = 3 ” Internal error (e.g. name too long)
OTHERS = 7
).
“
IF sy-subrc = 0.
lv_content_type = ‘application/json’.
lv_action_type = ‘GET’.
lo_http->request->set_content_type( lv_content_type ).
lo_http->request->set_method( lv_action_type ).
“The output shows a json format.
lo_http->request->set_header_field(
EXPORTING
name = ‘Accept’ ” Name of the header field
value = ‘application/json’ ” HTTP header field value
).
“To ignore the authentication during execution.
lo_http->propertytype_logon_popup = if_http_client=>co_disabled.
lo_http->authenticate(
EXPORTING
proxy_authentication = ‘ ‘ ” Proxy Logon (= ‘X’)
client = ‘800’ ” R/3 system (client number from logon)
username = ‘username’ ” ABAP System, User Logon Name
password = ‘password’ ” Logon ID
language = ‘E’ ” SAP System, Current Language
).
ENDIF.
lo_http->send(
EXPORTING
timeout = 15 ” Timeout of Answer Waiting Time
EXCEPTIONS
http_communication_failure = 1 ” Communication Error
http_invalid_state = 2 ” Invalid state
http_processing_failed = 3 ” Error when processing method
http_invalid_timeout = 4 ” Invalid Time Entry
OTHERS = 5
).
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
lo_http->receive(
EXCEPTIONS
http_communication_failure = 1 ” Communication Error
http_invalid_state = 2 ” Invalid state
http_processing_failed = 3 ” Error when processing method
OTHERS = 4
).
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*Get the response from target as character string cdata( ).
lv_result = lo_http->response->get_cdata( ).
lo_http->close( ).We have the response from target system and need to retrieve the data from the string variable lv_result. *Deserialize the data from Json to Abap
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_result ” JSON string
CHANGING
data = lr_data ” Data to serialize
).
lt_tab = VALUE #( ( fielname = ‘MATERIALCODE’)
( fielname = ‘USERNAME’)
( fielname = ‘MTART’ ) ).
ASSIGN lr_data->* TO <ls_data>.
ASSIGN COMPONENT ‘d’ OF STRUCTURE <ls_data> TO <ls_d>.
DATA(lo_typedescr) = cl_abap_typedescr=>describe_by_data( <ls_d> ).
ASSIGN <ls_d>->* TO <ls_struct>.
IF <ls_struct> IS ASSIGNED.
LOOP AT lt_tab INTO DATA(ls_tab).
ASSIGN COMPONENT ls_tab-fielname OF STRUCTURE <ls_struct> TO <lv_any>.
IF <lv_any> IS ASSIGNED.
ASSIGN <lv_any>->* TO <lv_char>.
IF <lv_char> IS ASSIGNED.
ASSIGN COMPONENT ls_tab-fielname OF STRUCTURE ls_material TO FIELD-SYMBOL(<lv_target>).
IF <lv_target> IS ASSIGNED.
<lv_target> = <lv_char>.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
UNASSIGN: <ls_data>,<ls_d>,<ls_struct>,<lv_any>,<lv_char>,<lv_target>.
cl_demo_output=>begin_section( ‘Material Detail’ ).
cl_demo_output=>write_data( ls_material ).
cl_demo_output=>next_section( ‘OData Service URL’ ).
cl_demo_output=>write_text( lv_url ).
cl_demo_output=>display( ).
CLEAR: ls_material.The output should be like below in debugger level. Test the above service URI to postman app in GET method.Note: In the example above, I retrieved data from an OData HTTP API that does not require an SSL certificate. However, when communication with external APIs. an SSL certificate is typically required. We will cover this in detail in a separate blog post.Thank you! Read More Technology Blog Posts by Members articles
#SAP
#SAPTechnologyblog