
data:
client_proxy TYPE REF TO /iwbep/if_cp_client_proxy,
function_resource type ref to /iwbep/if_cp_resource_function.
function_resource = client_proxy->create_resource_for_function( 'PROMOTE_EMPLOYEE')
https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/SalesOrder_Confirm?sap-client='002'&SalesOrderID='0500010020'
CLASS zact_test_call_action DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun .
PROTECTED SECTION.
PRIVATE SECTION.
METHODS execute_action
IMPORTING i_action_name TYPE string
i_query_parameters TYPE if_web_http_request=>name_value_pairs OPTIONAL
RETURNING VALUE(response) TYPE string
RAISING cx_http_dest_provider_error
cx_web_http_client_error .
ENDCLASS.
CLASS zact_test_call_action IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA query_parameters TYPE if_web_http_request=>name_value_pairs .
DATA action_name TYPE string.
query_parameters = VALUE #( ( name = 'SalesOrderID' value = '0500010020' ) ).
action_name = 'SalesOrder_Confirm'.
TRY.
DATA(response) = execute_action(
EXPORTING
i_action_name = action_name
i_query_parameters = query_parameters
).
out->write( |response: { response } | ).
CATCH cx_http_dest_provider_error INTO DATA(http_dest_provider_error).
"handle exception
out->write( http_dest_provider_error->get_longtext( ) ).
CATCH cx_web_http_client_error INTO DATA(web_http_client_error).
"handle exception
out->write( web_http_client_error->get_longtext( ) ).
ENDTRY.
ENDMETHOD.
METHOD execute_action.
DATA(demo_mode) = abap_false.
DATA http_header_fields TYPE if_web_http_request=>name_value_pairs .
http_header_fields = VALUE #( ( name = if_web_http_header=>accept value = |application/json| )
( name = if_web_http_header=>content_type value = |application/json| )
( name = 'x-csrf-token' value = 'fetch' ) ).
DATA(service_relative_url) = '/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/'.
DATA(sap_client) = '002'.
DATA(destination_name_in_dest_srv) = 'ES5'.
"Destination Required for Basic Authentication. Credentials are stored in the SAP BTP destination service
DATA(lo_http_destination) = cl_http_destination_provider=>create_by_cloud_destination(
i_name = 'ES5'
i_authn_mode = if_a4c_cp_service=>service_specific
).
DATA(relative_url) = |{ service_relative_url }?sap-client={ sap_client }|.
DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ).
"remove any white spaces since the method set_uri_path does not check for white spaces
CONDENSE relative_url NO-GAPS.
" GET CSRF token
http_client->get_http_request( )->set_uri_path( i_uri_path = relative_url ).
http_client->get_http_request( )->set_header_fields( http_header_fields ).
DATA(http_response) = http_client->execute( if_web_http_client=>get ). "--> works
DATA(http_status_code) = http_response->get_status( ).
DATA(x_csrf_token) = http_response->get_header_field( 'x-csrf-token' ).
DATA(http_response_body) = http_response->get_text( ).
" Prepare POST request
IF demo_mode = abap_false.
relative_url = |{ service_relative_url }{ i_action_name }?sap-client={ sap_client }&{ i_query_parameters[ 1 ]-name }= '{ i_query_parameters[ 1 ]-value }' |.
"remove any white spaces since the method set_uri_path does not check for white spaces
CONDENSE relative_url NO-GAPS.
ELSE.
EXIT.
ENDIF.
http_client->get_http_request( )->set_uri_path( i_uri_path = relative_url ).
http_header_fields = VALUE #( ( name = if_web_http_header=>accept value = |application/json| )
( name = 'x-csrf-token' value = x_csrf_token ) ).
http_client->get_http_request( )->set_header_fields( http_header_fields ).
* DATA(http_request_body) = | { add json string } |.
* http_client->get_http_request( )->set_text( http_request_body ).
http_response = http_client->execute( if_web_http_client=>post ).
http_status_code = http_response->get_status( ).
response = http_response->get_text( ).
ENDMETHOD.
ENDCLASS.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
14 | |
12 | |
12 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 |