Skip to Content
1
Aug 23, 2018 at 11:55 AM

Trigger SCP from ABAP program

1046 Views Last edit Aug 23, 2018 at 03:18 PM 4 rev

Hello Experts,

I am triggering a SCP workflow from ABAP system. Below is my code. I am able to get the CSRF token value successfully. But the actual instance creation is not happening I am getting 403 Forbidden error

Note: i am able trigger the instance using postman. so the workflow itself is working fine.

this is my code

*&---------------------------------------------------------------------*
*& Report Z_TRIGGER_SCP_WORFKLOW
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_TRIGGER_SCP_WORFKLOW.
DATA: lo_http_client TYPE REF TO if_http_client.
DATA: response TYPE string.
"create HTTP client by url
"API endpoint for API sandbox
CALL METHOD cl_http_client=>create_by_url
 EXPORTING
 url = 'https://bpmworkflowruntimeXXXXXea-XXXXX.hana.ondemand.com/workflow-service/rest/v1/xsrf-token'
 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.
"setting request method
lo_http_client->request->set_method('GET').
"adding headers
lo_http_client->request->set_header_field( name = 'X-CSRF-Token' value = 'Fetch' ).
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Authorization' value = 'Basic XXXXXXXXXXXXXXXXXXXXX' ).
"API Key for API Sandbox
*lo_http_client->request->set_header_field( name = 'APIKey' value = 'Vw0jBxyASfBvXXXXXXXFbEpcTKxizWKr' ).
"Available Security Schemes for productive API Endpoints
"Basic Authentication, OAuth 2.0
"Basic Auth : provide username:password in Base64 encoded in Authorization header
"lo_http_client->request->set_header_field( name = 'Authorization' value = 'Basic <Base64 encoded value>' ).
CALL METHOD lo_http_client->send
 EXCEPTIONS
 http_communication_failure = 1
 http_invalid_state = 2
 http_processing_failed = 3
 http_invalid_timeout = 4
 OTHERS = 5.
IF sy-subrc = 0.
 CALL METHOD lo_http_client->receive
 EXCEPTIONS
 http_communication_failure = 1
 http_invalid_state = 2
 http_processing_failed = 3
 OTHERS = 5.
ENDIF.
IF sy-subrc <> 0.
 "error handling
ENDIF.
data: lt_fields type TIHTTPNVP.
data(lo_response) = lo_http_client->response.
lo_response->IF_HTTP_ENTITY~GET_HEADER_FIELDS( CHANGING fields = lt_fields ).
*cl_demo_output=>display( lt_fields ).
data(ls_fields) = lt_fields[ name = 'x-csrf-token' ].
data(lv_csrf) = ls_fields-value.
CALL METHOD cl_http_client=>create_by_url
 EXPORTING
 url = 'https://bpmworkflowruntimeaXXXXXX-XXXXX.hana.ondemand.com/workflow-service/rest/v1/workflow-instances'
 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.
"setting request method
lo_http_client->request->set_method('POST').
"adding headers
lo_http_client->request->set_header_field( name = 'X-CSRF-Token' value = lv_csrf ).
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Authorization' value = 'Basic XXXXXXXXXXXXXXXXXXXXX' ).
CONCATENATE '{"definitionId": "myworkflowtest",'
 '"context": { "PreqItem": "00010", "Material": "000000000000000011",'
 '"Plant": 5000, "DelivDate": 25082018,"PrNumber": " " }}' into data(lv_json).
CALL METHOD lo_http_client->request->set_cdata
 EXPORTING
 data = lv_json.
CALL METHOD lo_http_client->send
 EXCEPTIONS
 http_communication_failure = 1
 http_invalid_state = 2
 http_processing_failed = 3
 http_invalid_timeout = 4
 OTHERS = 5.
IF sy-subrc = 0.
 CALL METHOD lo_http_client->receive
 EXCEPTIONS
 http_communication_failure = 1
 http_invalid_state = 2
 http_processing_failed = 3
 OTHERS = 5.
ENDIF.
IF sy-subrc <> 0.
 "error handling
ENDIF.
response = lo_http_client->response->get_cdata( ).
cl_demo_output=>display( response ).