CONSTANTS : RFCDEST TYPE RFCDEST value 'ECQ100XRBIA'.
DATA: LO_HTTP_request TYPE REF TO IF_HTTP_CLIENT,
lo_rest_client TYPE REF TO cl_rest_http_client,
lo_response TYPE REF TO if_rest_entity,
lv_http_status TYPE i,
lv_token TYPE string,
LV_SERVICE TYPE STRING.
DATA: lv_result TYPE string,
lv_body TYPE string.
CALL METHOD cl_http_client=>create_by_destination
EXPORTING
destination = RFCDEST
IMPORTING
client = LO_HTTP_request
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.
* create the URI for the client.
* l_query = im_query.
CALL METHOD cl_http_utility=>set_request_uri
EXPORTING
request = LO_HTTP_request->request
uri = 'https://my344355.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/LeadCollection?$top=1'. "l_query.
* update the HTTP Method
CALL METHOD LO_HTTP_request->request->set_method
EXPORTING
method = LO_HTTP_request->request->co_request_method_get.
* set Content type
CALL METHOD LO_HTTP_request->request->if_http_entity~set_content_type
EXPORTING
content_type = 'application/json'.
* set header field for fetching X-CSRF token
CALL METHOD LO_HTTP_request->request->set_header_field
EXPORTING
name = 'X-CSRF-Token'
value = 'Fetch'.
CALL METHOD LO_HTTP_request->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ). "Send the HTTP request
CALL METHOD LO_HTTP_request->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ). "receive the response
****GET x-csrf TOKEN from earlier response
CALL METHOD LO_HTTP_request->response->get_header_field
EXPORTING
name = 'X-CSRF-Token'
RECEIVING
value = lv_token.
* Set X-CSRF- Token in the new request.
CALL METHOD LO_HTTP_request->request->set_header_field
EXPORTING
name = 'X-CSRF-Token'
value = lv_token.
* update the HTTP Method
CALL METHOD LO_HTTP_request->request->set_method
EXPORTING
method = LO_HTTP_request->request->co_request_method_post.
****content type
CALL METHOD LO_HTTP_request->request->set_content_type
EXPORTING
content_type = 'application/json'.
* create Body for the HTTP Post request
DATA lv_json_post_data TYPE string.
lv_json_post_data = | \{ | &&
| "Name":"DVLead10003 KS Divya", | &&
| "AccountPartyID":"XR100", | &&
| \} |.
*CONCATENATE
CALL METHOD LO_HTTP_request->request->set_cdata
EXPORTING
data = lv_json_post_data.
CALL METHOD LO_HTTP_request->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ). "Send the HTTP request
CALL METHOD LO_HTTP_request->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ). "receive the response
lv_result = LO_HTTP_request->response->get_cdata( ).
write lv_result.