{"id":7919,"date":"2025-10-12T00:44:13","date_gmt":"2025-10-11T19:14:13","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=api-call-http"},"modified":"2025-10-12T00:44:13","modified_gmt":"2025-10-11T19:14:13","slug":"api-call-http","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=api-call-http","title":{"rendered":"API call &#8211; HTTP"},"content":{"rendered":"<p>This ABAP class is designed for <strong>SAP BTP ABAP Environment<\/strong> to connect securely to an external OData or REST API (for example, an EHS Raw Material service).<br \/>It demonstrates:<\/p>\n<ul>\n<li>Secure HTTP communication via <strong>BTP destinations<\/strong><\/li>\n<li>CSRF token handling<\/li>\n<li>JSON serialization\/deserialization<\/li>\n<li>Clean separation between connection setup and data retrieval<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><strong>Code:<\/p>\n<p><\/strong><\/span><\/p>\n<p>CLASS \/iap\/cl_intelligent_matching DEFINITION<br \/>\nPUBLIC FINAL<br \/>\nCREATE PUBLIC.<\/p>\n<p>PUBLIC SECTION.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&#8221; Structure for API Response (Raw Material Compliance Info)<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nTYPES: BEGIN OF ty_rawmat_result,<br \/>\nuuid TYPE string, &#8221; Unique ID of the record<br \/>\nmaterial_id TYPE string, &#8221; Material number<br \/>\nmaterial_name TYPE string, &#8221; Material description<br \/>\nmaterial_group TYPE string, &#8221; Material group code<br \/>\nmaterial_group_name TYPE string, &#8221; Material group name<br \/>\nis_active TYPE abap_bool, &#8221; Active status flag<br \/>\nEND OF ty_rawmat_result.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&#8221; Public Methods<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nCLASS-METHODS:<br \/>\ncreate_client_proxy<br \/>\nRETURNING VALUE(ro_client_proxy) TYPE REF TO \/iwbep\/if_cp_client_proxy,<\/p>\n<p>get_rawmat_data<br \/>\nRETURNING VALUE(rt_rawmat_data) TYPE STANDARD TABLE OF ty_rawmat_result,<\/p>\n<p>convert_to_json<br \/>\nIMPORTING it_data TYPE any<br \/>\nEXPORTING ev_json TYPE string.<\/p>\n<p>PRIVATE SECTION.<br \/>\nCONSTANTS:<br \/>\nc_destination TYPE string VALUE &#8216;DEMO_EHS_DEST&#8217;. &#8221; Destination configured in BTP Cockpit<\/p>\n<p>ENDCLASS.<\/p>\n<p>CLASS \/iap\/cl_intelligent_matching IMPLEMENTATION.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&#8221; 1. Create OData V4 Client Proxy<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nMETHOD create_client_proxy.<\/p>\n<p>TRY.<br \/>\n&#8221; Create destination object from BTP destination service<br \/>\nDATA(lo_destination) = cl_http_destination_provider=&gt;create_by_cloud_destination(<br \/>\ni_name = c_destination<br \/>\ni_authn_mode = if_a4c_cp_service=&gt;service_specific ).<\/p>\n<p>&#8221; Create HTTP client for communication<br \/>\nDATA(lo_http_client) = cl_web_http_client_manager=&gt;create_by_http_destination( lo_destination ).<\/p>\n<p>&#8221; Create remote OData V4 proxy for the target service<br \/>\nro_client_proxy = \/iwbep\/cl_cp_factory_remote=&gt;create_v4_remote_proxy(<br \/>\nis_proxy_model_key = VALUE #(<br \/>\nrepository_id = &#8216;DEFAULT&#8217;<br \/>\nproxy_model_id = &#8216;\/IAP\/CHM_SCM_IM&#8217;<br \/>\nproxy_model_version = &#8216;0001&#8217; )<br \/>\nio_http_client = lo_http_client<br \/>\niv_relative_service_root = &#8216;\/sap\/opu\/odata\/sap\/ZDEMO_EHS_SRV\/&#8217; ). &#8221; Demo endpoint<\/p>\n<p>CATCH cx_http_dest_provider_error INTO DATA(lx_dest_err).<br \/>\nMESSAGE lx_dest_err-&gt;get_text( ) TYPE &#8216;E&#8217;.<br \/>\nCATCH cx_root INTO DATA(lx_root).<br \/>\nMESSAGE lx_root-&gt;get_text( ) TYPE &#8216;E&#8217;.<br \/>\nENDTRY.<\/p>\n<p>ENDMETHOD.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&#8221; 2. Fetch Raw Material Data from External API<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nMETHOD get_rawmat_data.<\/p>\n<p>DATA: lo_destination TYPE REF TO if_http_destination,<br \/>\nlo_http_client TYPE REF TO if_web_http_client,<br \/>\nlo_response TYPE REF TO if_web_http_response,<br \/>\nlv_json TYPE string,<br \/>\nlv_http_status TYPE i.<\/p>\n<p>TRY.<br \/>\n&#8221; Obtain HTTP destination (configured in BTP cockpit)<br \/>\nlo_destination = cl_http_destination_provider=&gt;create_by_cloud_destination(<br \/>\ni_name = c_destination<br \/>\ni_authn_mode = if_a4c_cp_service=&gt;service_specific ).<\/p>\n<p>lo_http_client = cl_web_http_client_manager=&gt;create_by_http_destination( lo_destination ).<\/p>\n<p>&#8221; === Step 1: Fetch CSRF Token ===<br \/>\nDATA(lo_req_token) = lo_http_client-&gt;get_http_request( ).<br \/>\nlo_req_token-&gt;set_header_field( name = &#8216;X-CSRF-Token&#8217; value = &#8216;Fetch&#8217; ).<\/p>\n<p>DATA(lo_resp_token) = lo_http_client-&gt;execute( if_web_http_client=&gt;get ).<br \/>\nDATA(lv_csrf_token) = lo_resp_token-&gt;get_header_field( &#8216;X-CSRF-Token&#8217; ).<\/p>\n<p>&#8221; === Step 2: Perform GET Request to Demo API ===<br \/>\nDATA(lo_req_get) = lo_http_client-&gt;get_http_request( ).<br \/>\nlo_req_get-&gt;set_header_field( name = &#8216;X-CSRF-Token&#8217; value = lv_csrf_token ).<br \/>\nlo_req_get-&gt;set_header_field( name = &#8216;Accept&#8217; value = &#8216;application\/json&#8217; ).<\/p>\n<p>lo_response = lo_http_client-&gt;execute(<br \/>\ni_method = if_web_http_client=&gt;get<br \/>\ni_relative_url = &#8216;\/sap\/opu\/odata\/sap\/ZDEMO_EHS_SRV\/C_RawMatInfoSet&#8217; ). &#8221; Demo path<\/p>\n<p>lv_http_status = lo_response-&gt;get_status( ).<\/p>\n<p>IF lv_http_status = 200.<br \/>\nlv_json = lo_response-&gt;get_text( ).<\/p>\n<p>\/ui2\/cl_json=&gt;deserialize(<br \/>\nEXPORTING json = lv_json<br \/>\nCHANGING data = rt_rawmat_data ).<\/p>\n<p>ELSE.<br \/>\nMESSAGE |HTTP Error { lv_http_status }: { lo_response-&gt;get_status_text( ) }| TYPE &#8216;E&#8217;.<br \/>\nENDIF.<\/p>\n<p>CATCH cx_http_dest_provider_error INTO DATA(lx_dest_error).<br \/>\nMESSAGE lx_dest_error-&gt;get_text( ) TYPE &#8216;E&#8217;.<\/p>\n<p>CATCH cx_web_http_client_error INTO DATA(lx_http_error).<br \/>\nMESSAGE lx_http_error-&gt;get_text( ) TYPE &#8216;E&#8217;.<\/p>\n<p>CATCH cx_root INTO DATA(lx_any).<br \/>\nMESSAGE lx_any-&gt;get_text( ) TYPE &#8216;E&#8217;.<br \/>\nENDTRY.<\/p>\n<p>ENDMETHOD.<\/p>\n<p>&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n&#8221; 3. Convert Any ABAP Data Structure to JSON String<br \/>\n&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nMETHOD convert_to_json.<br \/>\nev_json = \/ui2\/cl_json=&gt;serialize(<br \/>\nEXPORTING<br \/>\ndata = it_data<br \/>\nconversion_exits = abap_true<br \/>\npretty_name = \/ui2\/cl_json=&gt;pretty_mode-camel_case ).<br \/>\nENDMETHOD.<\/p>\n<p>ENDCLASS.<\/p>\n<p><span style=\"text-decoration: underline\"><strong>\u00a0<\/strong><\/span><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,222,442,213,177],"class_list":["post-7919","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-api","question_tags-http","question_tags-reports","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7919"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7919"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7919"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}