-
-
Save SidVal/65da5cb2c96eca6bc02ad835c0a7ff08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* 1- Instantiate the HTTP Client in ABAP | |
* Create the HTTP CLient | |
CALL METHOD cl_http_client=>create_by_destination | |
EXPORTING | |
destination = c_rfc | |
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. | |
* 2- Fill headers and set URI for GET Method | |
* create the URI for the client. | |
l_query = '' ."c_query. | |
CALL METHOD cl_http_utility=>set_request_uri | |
EXPORTING | |
request = lo_http_client->request | |
uri = l_query. | |
* update the HTTP Method | |
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/json'. | |
* set header field for fetching X-CSRF token | |
CALL METHOD lo_http_client->request->set_header_field | |
EXPORTING | |
name = 'X-CSRF-Token' | |
value = 'Fetch'. | |
* 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 | |
CALL METHOD lo_http_client->response->get_header_field | |
EXPORTING | |
name = 'X-CSRF-Token' | |
RECEIVING | |
value = l_token. | |
* 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 = 'X-CSRF-Token' | |
value = l_token. | |
* 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/json'. | |
* create Body for the HTTP Post request | |
CALL METHOD lo_http_client->request->set_cdata | |
EXPORTING | |
data = lv_json. | |
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(). | |
CALL METHOD lo_http_client->RESPONSE->GET_CDATA | |
RECEIVING DATA = l_result . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment