{"id":7922,"date":"2025-10-12T00:46:25","date_gmt":"2025-10-11T19:16:25","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=post-api-call"},"modified":"2025-10-12T00:46:25","modified_gmt":"2025-10-11T19:16:25","slug":"post-api-call","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=post-api-call","title":{"rendered":"Post API call"},"content":{"rendered":"<ul>\n<li>Use the same destination (<code>DEMO_EHS_DEST<\/code>)<\/li>\n<li>Fetch a CSRF token<\/li>\n<li>Serialize an ABAP structure into JSON<\/li>\n<li>Send it as the POST body to the demo OData endpoint<\/li>\n<li>Handle the response cleanly<\/li>\n<\/ul>\n<p>CLASS \/iap\/cl_intelligent_matching DEFINITION<br \/>\nPUBLIC FINAL<br \/>\nCREATE PUBLIC.<\/p>\n<p>PUBLIC SECTION.<\/p>\n<p>TYPES: BEGIN OF ty_rawmat_result,<br \/>\nuuid TYPE string,<br \/>\nmaterial_id TYPE string,<br \/>\nmaterial_name TYPE string,<br \/>\nmaterial_group TYPE string,<br \/>\nmaterial_group_name TYPE string,<br \/>\nis_active TYPE abap_bool,<br \/>\nEND OF ty_rawmat_result.<\/p>\n<p>CLASS-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>post_rawmat_data<br \/>\nIMPORTING is_payload TYPE ty_rawmat_result<br \/>\nRETURNING VALUE(rv_status_text) TYPE string,<\/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 name 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 (for typed OData calls if needed)<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 \/>\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>DATA(lo_http_client) = cl_web_http_client_manager=&gt;create_by_http_destination( lo_destination ).<\/p>\n<p>ro_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_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. GET Request &#8211; Fetch Raw Material Data<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_status TYPE i.<\/p>\n<p>TRY.<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; === 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; === Call the Demo OData API (GET) ===<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; ).<\/p>\n<p>lv_status = lo_response-&gt;get_status( ).<\/p>\n<p>IF lv_status = 200.<br \/>\nlv_json = lo_response-&gt;get_text( ).<br \/>\n\/ui2\/cl_json=&gt;deserialize(<br \/>\nEXPORTING json = lv_json<br \/>\nCHANGING data = rt_rawmat_data ).<br \/>\nELSE.<br \/>\nMESSAGE |HTTP Error { lv_status }: { lo_response-&gt;get_status_text( ) }| TYPE &#8216;E&#8217;.<br \/>\nENDIF.<\/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. POST Request &#8211; Create a new Raw Material record<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 post_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_body TYPE string,<br \/>\nlv_csrf_token TYPE string,<br \/>\nlv_status_code TYPE i.<\/p>\n<p>TRY.<br \/>\n&#8221; === Create destination ===<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; === 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; ).<br \/>\nDATA(lo_resp_token) = lo_http_client-&gt;execute( if_web_http_client=&gt;get ).<br \/>\nlv_csrf_token = lo_resp_token-&gt;get_header_field( &#8216;X-CSRF-Token&#8217; ).<\/p>\n<p>&#8221; === Serialize ABAP structure to JSON ===<br \/>\nlv_json_body = \/ui2\/cl_json=&gt;serialize(<br \/>\nEXPORTING<br \/>\ndata = is_payload<br \/>\npretty_name = \/ui2\/cl_json=&gt;pretty_mode-camel_case<br \/>\nconversion_exits = abap_true ).<\/p>\n<p>&#8221; === Prepare POST Request ===<br \/>\nDATA(lo_req_post) = lo_http_client-&gt;get_http_request( ).<br \/>\nlo_req_post-&gt;set_header_field( name = &#8216;X-CSRF-Token&#8217; value = lv_csrf_token ).<br \/>\nlo_req_post-&gt;set_header_field( name = &#8216;Content-Type&#8217; value = &#8216;application\/json&#8217; ).<br \/>\nlo_req_post-&gt;set_header_field( name = &#8216;Accept&#8217; value = &#8216;application\/json&#8217; ).<br \/>\nlo_req_post-&gt;set_text( lv_json_body ).<\/p>\n<p>&#8221; === Execute POST ===<br \/>\nlo_response = lo_http_client-&gt;execute(<br \/>\ni_method = if_web_http_client=&gt;post<br \/>\ni_relative_url = &#8216;\/sap\/opu\/odata\/sap\/ZDEMO_EHS_SRV\/C_RawMatInfoSet&#8217; ).<\/p>\n<p>lv_status_code = lo_response-&gt;get_status( ).<br \/>\nrv_status_text = |{ lv_status_code } { lo_response-&gt;get_status_text( ) }|.<\/p>\n<p>CATCH cx_root INTO DATA(lx_any).<br \/>\nrv_status_text = lx_any-&gt;get_text( ).<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; 4. JSON Serializer Utility<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","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,222,442,443,177],"class_list":["post-7922","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-api","question_tags-http","question_tags-post-call","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7922","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=7922"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7922"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7922"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}