REPORT zmm_api_load8_final NO STANDARD PAGE HEADING.
DATA: l_query TYPE string,
l_body(5000) TYPE c,
l_token TYPE string,
l_result TYPE string.
DATA: lo_http_client TYPE REF TO if_http_client.
DATA lo_client TYPE REF TO if_http_client.
*Data variables for storing response in xstring and string
DATA : lv_xstring TYPE xstring,
lv_string TYPE string,
lv_node_name TYPE string.
CLEAR : lv_xstring, lv_string, lv_node_name.
DATA : http_client TYPE REF TO if_http_client.
DATA : l_str_length TYPE i.
DATA : mime TYPE w3mimetabtype.
CONSTANTS: c_rfchana TYPE rfcdest VALUE 'NONE'. " rfc Destination
"c_query TYPE string VALUE 'MaterialEventSet'. "” Entity name
*CALL METHOD cl_http_client=>create_by_destination
* EXPORTING
* destination = c_rfchana
* IMPORTING
* client = lo_http_client
* EXCEPTIONS
* argument_not_found = 1
* destination_not_found = 2
* destination_no_authority = 3
* plugin_not_active = 4
* internal_error = 5
* OTHERS = 6.
*
*IF NOT sy-subrc IS INITIAL.
*
*ENDIF.
CONSTANTS: lv_initial_url TYPE string VALUE 'http://dserver1.access.ly/madeena.catalyst.801/API/123456789/CreateItem/'.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_initial_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
"error handling
ENDIF.
"STEP 2:- Fill headers and set URI for GET Method
* create the URI for the client.
*l_query = 'https://10.50.40.18:8010/sap/opu/odata/sap/ZMM_MATERIAL_TRIGGER_SRV/MaterialEventSet'.
*l_query = 'http://dserver1.access.ly/madeena.catalyst.801/API/123456789/CreateItem/'.
*
*CALL METHOD cl_http_utility=>set_request_uri
* EXPORTING
* request = lo_http_client->request
* uri = l_query.
* update the HTTP Method
lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled.
CALL METHOD lo_http_client->request->set_method
EXPORTING
method = lo_http_client->request->co_request_method_get.
* set Content type
CALL METHOD lo_http_client->request->if_http_entity~set_content_type
EXPORTING
content_type = 'application/xml'.
*lo_http_client->request->set_header_field(
* EXPORTING
* name = 'Authorization' " Name of the header field
* value = 'f0bb13aa-9b94-4510-90b8-247f2c494939' " HTTP header field value
* ).
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Authorization'
value = 'f0bb13aa-9b94-4510-90b8-247f2c494939'.
* set header field for fetching X-CSRF token
*CALL METHOD lo_http_client->request->set_header_field
* EXPORTING
* name = /iwfnd/if_oci_common=>gc_xcsrf_token
* value = 'Fetch'.
*CALL METHOD lo_http_client->request->set_header_field
* EXPORTING
* name = 'sap-client'
* value = '200'.
"Step 3:- Trigger the GET Method
*lo_http_client->send(
*
* EXCEPTIONS
*
* http_communication_failure = 1
*
* http_invalid_state = 2 ). "“send the http request
*
*lo_http_client->receive(
*
*EXCEPTIONS
*
*http_communication_failure = 1
*
*http_invalid_state = 2
*
*http_processing_failed = 3 ). "“receive the response
****GET x-csrf TOKEN from earlier response
lv_xstring = lo_http_client->response->get_data( ).
l_str_length = xstrlen( lv_xstring ).
CALL FUNCTION 'RSFO_XSTRING_TO_MIME'
EXPORTING
c_xstring = lv_xstring
i_length = l_str_length
TABLES
c_t_mime = mime.
DATA: lv_body TYPE string.
CALL FUNCTION 'CRM_IC_XML_XSTRING2STRING'
EXPORTING
inxstring = lv_xstring
IMPORTING
outstring = lv_body.
DATA : lv_payload TYPE string.
CLEAR l_body.
DATA: lv_mat TYPE string.
lv_mat = '200044'.
CONCATENATE '<?xml version="1.0"?>'
'<Stock>'
'<ItemID>'lv_mat'</ItemID>'
'<ItemName>4G Drink1</ItemName>'
'<ItemName_AR />'
'<CategoryCode>DA0001</CategoryCode>'
'<ForPOS>1</ForPOS>'
'<TaxPer>0</TaxPer>'
'<Blocked>0</Blocked>'
'<ItemType>I</ItemType>'
'<RewardPoints>20</RewardPoints>'
'<Flag>E</Flag>'
'<Subunit>'
'<Barcode>8880088</Barcode>'
'<ItemName>4G Drink1</ItemName>'
'<Ismain>1</Ismain>'
'<Unit>EA</Unit>'
'<Scale>1</Scale>'
'<ScaleString>6/1</ScaleString>'
'<WScaleType>P</WScaleType>'
'<ISPOS_SP>0</ISPOS_SP>'
'<SpLevel_Price>'
'<SPID>01</SPID>'
'<SP_with_VAT>500</SP_with_VAT>'
'</SpLevel_Price>'
'</Subunit>'
'</Stock>'
INTO l_body RESPECTING BLANKS.
* DATA : lt_string_tab TYPE string_table.
* CLEAR lt_string_tab.
*SPLIT lv_payload AT '>' INTO TABLE lt_string_tab.
*
*LOOP AT lt_string_tab INTO DATA(ls_string).
* WRITE / : ls_string '>'.
*ENDLOOP.
*
*LOOP AT lt_string INTO ls_str.
*
*WRITE: ls_str-lv_data.
*
*ENDLOOP.
WRITE : / '================================================= REQUEST============================================='.
DATA lt_string_tab TYPE string_table.
SPLIT l_body AT '>' INTO TABLE lt_string_tab.
*
*LOOP AT lt_string_tab INTO DATA(ls_string).
* WRITE / : ls_string &&'>'.
*ENDLOOP.
*CALL METHOD lo_http_client->response->get_header_field
* EXPORTING
* name = /iwfnd/if_oci_common=>gc_xcsrf_token
* RECEIVING
* value = l_token.
*CALL METHOD lo_http_client->request->set_header_field
* EXPORTING
* name = 'sap-client'
* value = '200'.
"Step 4:- Fill headers and Body for HTTP POST method
* Set X-CSRF- Token in the new request.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Authorization'
value = 'f0bb13aa-9b94-4510-90b8-247f2c494939'.
* update the HTTP Method
CALL METHOD lo_http_client->request->set_method
EXPORTING
method = lo_http_client->request->co_request_method_post.
****content type
CALL METHOD lo_http_client->request->set_content_type
EXPORTING
content_type = 'application/xml'.
* create Body for the HTTP Post request
CALL METHOD lo_http_client->request->set_cdata
EXPORTING
data = CONV #( l_body ).
lo_http_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ). "“send the http request
lo_http_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ). "receive the response
l_result = lo_http_client->response->get_cdata( ).
WRITE : / '================================================= RESPONSE ============================================='.
*DATA lt_string_tab TYPE string_table.
CLEAR lt_string_tab.
SPLIT l_result AT '>' INTO TABLE lt_string_tab.
LOOP AT lt_string_tab INTO DATA(ls_string).
FIND '<Status' IN ls_string IGNORING CASE.
* WRITE / : ls_string && '>'.
* SEARCH ls_string FOR 'Status'.
IF sy-subrc EQ 0.
DATA(lv_index) = sy-tabix.
lv_index = lv_index + 1.
READ TABLE lt_string_tab INTO DATA(ls_string2) INDEX lv_index.
WRITE: / ls_string .
WRITE: / ls_string2.
* CLEAR:ls_string.
ENDIF.
ENDLOOP.
FIND '<Status' IN l_result IGNORING CASE.
IF sy-subrc EQ 0.
ENDIF.
lo_http_client->close( ).
Output :