Communication with another external system is mostly carried out using HTTP requests. SOAP and REST API services can be accessed in this manner.
Do not write each HTTP request into the code. This is a constantly needed situation. To avoid code repetition, a function or method should be created. It should be called when needed.
The following example function can be used for HTTP requests.
function zhttp_request.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IV_URL) TYPE STRING
*" REFERENCE(IV_METHOD) TYPE STRING
*" VALUE(IV_DATA) TYPE STRING OPTIONAL
*" REFERENCE(IV_CONTENT_TYPE) TYPE STRING DEFAULT
*" 'application/json'
*" EXPORTING
*" REFERENCE(EV_STATUS_CODE) TYPE I
*" REFERENCE(EV_RESPONSE) TYPE STRING
*" EXCEPTIONS
*" RECEIVE_EXCETION
*"----------------------------------------------------------------------
data : lo_client type ref to if_http_client,
lo_rest type ref to if_rest_client,
lo_entity type ref to if_rest_entity.
clear: lo_client, lo_rest.
cl_http_client=>create_by_url(
exporting
url = iv_url
ssl_id = 'ANONYM'
importing
client = lo_client
exceptions
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
others = 4 ).
lo_rest = new cl_rest_http_client( lo_client ).
lo_entity = lo_rest->create_request_entity( ).
lo_entity->set_content_type( iv_content_type ).
lo_entity->set_string_data( iv_data ).
case iv_method.
when 'GET'.
lo_rest->get( ).
when 'POST'.
lo_rest->post( lo_entity ).
endcase.
data(lv_staus_code) = lo_rest->get_status( ).
data(lv_response) = lo_rest->get_response_entity( )->get_string_data( ).
ev_status_code = lv_staus_code.
ev_response = lv_response.
endfunction.