Hello,

Sign up to join our community!

Welcome Back,

Please sign in to your account!

Forgot Password,

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

SAP EWM Help Latest Questions

  • 0
  • 0
DPM125
Beginner

Post API call

  • Use the same destination (DEMO_EHS_DEST)
  • Fetch a CSRF token
  • Serialize an ABAP structure into JSON
  • Send it as the POST body to the demo OData endpoint
  • Handle the response cleanly

CLASS /iap/cl_intelligent_matching DEFINITION
PUBLIC FINAL
CREATE PUBLIC.

PUBLIC SECTION.

TYPES: BEGIN OF ty_rawmat_result,
uuid TYPE string,
material_id TYPE string,
material_name TYPE string,
material_group TYPE string,
material_group_name TYPE string,
is_active TYPE abap_bool,
END OF ty_rawmat_result.

CLASS-METHODS:
create_client_proxy
RETURNING VALUE(ro_client_proxy) TYPE REF TO /iwbep/if_cp_client_proxy,

get_rawmat_data
RETURNING VALUE(rt_rawmat_data) TYPE STANDARD TABLE OF ty_rawmat_result,

post_rawmat_data
IMPORTING is_payload TYPE ty_rawmat_result
RETURNING VALUE(rv_status_text) TYPE string,

convert_to_json
IMPORTING it_data TYPE any
EXPORTING ev_json TYPE string.

PRIVATE SECTION.
CONSTANTS:
c_destination TYPE string VALUE ‘DEMO_EHS_DEST’. ” Destination name in BTP Cockpit

ENDCLASS.

CLASS /iap/cl_intelligent_matching IMPLEMENTATION.

“———————————————————————-
” 1. Create OData V4 Client Proxy (for typed OData calls if needed)
“———————————————————————-
METHOD create_client_proxy.

TRY.
DATA(lo_destination) = cl_http_destination_provider=>create_by_cloud_destination(
i_name = c_destination
i_authn_mode = if_a4c_cp_service=>service_specific ).

DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).

ro_client_proxy = /iwbep/cl_cp_factory_remote=>create_v4_remote_proxy(
is_proxy_model_key = VALUE #(
repository_id = ‘DEFAULT’
proxy_model_id = ‘/IAP/CHM_SCM_IM’
proxy_model_version = ‘0001’ )
io_http_client = lo_http_client
iv_relative_service_root = ‘/sap/opu/odata/sap/ZDEMO_EHS_SRV/’ ). ” Demo endpoint

CATCH cx_root INTO DATA(lx_root).
MESSAGE lx_root->get_text( ) TYPE ‘E’.
ENDTRY.

ENDMETHOD.

“———————————————————————-
” 2. GET Request – Fetch Raw Material Data
“———————————————————————-
METHOD get_rawmat_data.

DATA: lo_destination TYPE REF TO if_http_destination,
lo_http_client TYPE REF TO if_web_http_client,
lo_response TYPE REF TO if_web_http_response,
lv_json TYPE string,
lv_status TYPE i.

TRY.
lo_destination = cl_http_destination_provider=>create_by_cloud_destination(
i_name = c_destination
i_authn_mode = if_a4c_cp_service=>service_specific ).

lo_http_client = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).

” === Fetch CSRF Token ===
DATA(lo_req_token) = lo_http_client->get_http_request( ).
lo_req_token->set_header_field( name = ‘X-CSRF-Token’ value = ‘Fetch’ ).

DATA(lo_resp_token) = lo_http_client->execute( if_web_http_client=>get ).
DATA(lv_csrf_token) = lo_resp_token->get_header_field( ‘X-CSRF-Token’ ).

” === Call the Demo OData API (GET) ===
DATA(lo_req_get) = lo_http_client->get_http_request( ).
lo_req_get->set_header_field( name = ‘X-CSRF-Token’ value = lv_csrf_token ).
lo_req_get->set_header_field( name = ‘Accept’ value = ‘application/json’ ).

lo_response = lo_http_client->execute(
i_method = if_web_http_client=>get
i_relative_url = ‘/sap/opu/odata/sap/ZDEMO_EHS_SRV/C_RawMatInfoSet’ ).

lv_status = lo_response->get_status( ).

IF lv_status = 200.
lv_json = lo_response->get_text( ).
/ui2/cl_json=>deserialize(
EXPORTING json = lv_json
CHANGING data = rt_rawmat_data ).
ELSE.
MESSAGE |HTTP Error { lv_status }: { lo_response->get_status_text( ) }| TYPE ‘E’.
ENDIF.

CATCH cx_root INTO DATA(lx_any).
MESSAGE lx_any->get_text( ) TYPE ‘E’.
ENDTRY.

ENDMETHOD.

“———————————————————————-
” 3. POST Request – Create a new Raw Material record
“———————————————————————-
METHOD post_rawmat_data.

DATA: lo_destination TYPE REF TO if_http_destination,
lo_http_client TYPE REF TO if_web_http_client,
lo_response TYPE REF TO if_web_http_response,
lv_json_body TYPE string,
lv_csrf_token TYPE string,
lv_status_code TYPE i.

TRY.
” === Create destination ===
lo_destination = cl_http_destination_provider=>create_by_cloud_destination(
i_name = c_destination
i_authn_mode = if_a4c_cp_service=>service_specific ).

lo_http_client = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).

” === Fetch CSRF Token ===
DATA(lo_req_token) = lo_http_client->get_http_request( ).
lo_req_token->set_header_field( name = ‘X-CSRF-Token’ value = ‘Fetch’ ).
DATA(lo_resp_token) = lo_http_client->execute( if_web_http_client=>get ).
lv_csrf_token = lo_resp_token->get_header_field( ‘X-CSRF-Token’ ).

” === Serialize ABAP structure to JSON ===
lv_json_body = /ui2/cl_json=>serialize(
EXPORTING
data = is_payload
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
conversion_exits = abap_true ).

” === Prepare POST Request ===
DATA(lo_req_post) = lo_http_client->get_http_request( ).
lo_req_post->set_header_field( name = ‘X-CSRF-Token’ value = lv_csrf_token ).
lo_req_post->set_header_field( name = ‘Content-Type’ value = ‘application/json’ ).
lo_req_post->set_header_field( name = ‘Accept’ value = ‘application/json’ ).
lo_req_post->set_text( lv_json_body ).

” === Execute POST ===
lo_response = lo_http_client->execute(
i_method = if_web_http_client=>post
i_relative_url = ‘/sap/opu/odata/sap/ZDEMO_EHS_SRV/C_RawMatInfoSet’ ).

lv_status_code = lo_response->get_status( ).
rv_status_text = |{ lv_status_code } { lo_response->get_status_text( ) }|.

CATCH cx_root INTO DATA(lx_any).
rv_status_text = lx_any->get_text( ).
ENDTRY.

ENDMETHOD.

“———————————————————————-
” 4. JSON Serializer Utility
“———————————————————————-
METHOD convert_to_json.
ev_json = /ui2/cl_json=>serialize(
EXPORTING
data = it_data
conversion_exits = abap_true
pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
ENDMETHOD.

ENDCLASS.

Related Questions

Leave an answer

Leave an answer