Skip to content

Instantly share code, notes, and snippets.

@SidVal

SidVal/Con token Secret

Created May 23, 2019 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SidVal/65da5cb2c96eca6bc02ad835c0a7ff08 to your computer and use it in GitHub Desktop.
Save SidVal/65da5cb2c96eca6bc02ad835c0a7ff08 to your computer and use it in GitHub Desktop.
* 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 .
* 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.
IF NOT sy-subrc IS INITIAL.
ENDIF.
data: lv_status TYPE i,
lt_fields TYPE tihttpnvp,
lv_sysubrc TYPE sysubrc.
lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled.
CALL METHOD lo_http_client->request->set_method( if_http_request=>co_request_method_get ).
lo_http_client->request->set_header_field( name = 'x-csrf-token' value = 'Fetch' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Authorization' value = 'Your basic authentication' ).
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
ASSERT sy-subrc = 0.
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
IF sy-subrc <> 0.
CALL METHOD lo_http_client->get_last_error
IMPORTING
code = lv_sysubrc
message = DATA(ev_message).
WRITE: / 'Error when getting token:', ev_message.
RETURN.
ENDIF.
lo_http_client->response->get_header_fields( CHANGING fields = lt_fields ).
data: ev_token type string,
et_cookies TYPE tihttpcki.
READ TABLE lt_fields ASSIGNING FIELD-SYMBOL(<field>) WITH KEY name = 'x-csrf-token'.
ev_token = <field>-value.
lo_http_client->response->get_cookies( CHANGING cookies = et_cookies ).
lo_http_client->close( ).
"create opp
lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled.
CALL METHOD lo_http_client->request->set_method(
if_http_request=>co_request_method_post ).
DATA it_cookies TYPE tihttpcki.
DATA: c_newline(1) TYPE c VALUE cl_abap_char_utilities=>newline.
DATA: v_json TYPE string.
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'multipart/mixed; boundary=batch_1' ).
lo_http_client->request->set_header_field( name = 'x-csrf-token' value = l_token ).
lo_http_client->request->set_header_field( name = 'Authorization' value = 'application/json' ).
LOOP AT et_cookies ASSIGNING FIELD-SYMBOL(<cookie>).
lo_http_client->request->set_cookie( name = <cookie>-name
value = <cookie>-value ).
ENDLOOP.
* CONCATENATE '--batch_1' c_newline into v_json.
* CONCATENATE v_json 'Content-Type: multipart/mixed; boundary=changeset_1' c_newline into v_json.
**
* CONCATENATE v_json '--changeset_1' c_newline into v_json.
* CONCATENATE v_json 'Content-Type: application/http' c_newline into v_json.
* CONCATENATE v_json 'Content-Transfer-Encoding: binary' c_newline into v_json.
*
* CONCATENATE v_json 'POST OpportunityCollection HTTP/1.1' c_newline into v_json.
* CONCATENATE v_json 'Content-Length: 5000' c_newline into v_json.
* CONCATENATE v_json 'Accept: application/json' c_newline into v_json.
* CONCATENATE v_json 'Content-Type: application/json' c_newline into v_json.
*
* CONCATENATE v_json lv_json c_newline into v_json.
* CONCATENATE v_json '--changeset_1--' c_newline into v_json.
* CONCATENATE v_json '--batch_1--' c_newline into v_json.
l_body = lv_json. "l_body && cl_abap_char_utilities=>cr_lf.
lo_http_client->request->set_cdata( data = l_body ).
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
ASSERT sy-subrc = 0.
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
DATA(lv_resp) = lo_http_client->response->get_cdata( ).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment