Hi Experts,
I americium getting mistake "CSRF Token Validation Failed" successful POST petition API of my SAP strategy since I'm generating CSRF token utilizing GET petition and walk the aforesaid to POST petition successful aforesaid SAP investigating client.
I americium capable to make CSRF token successfully done beneath codification utilizing get_header_field thru GET request. Also, I' m passing the aforesaid token to get_header_field thru POST petition with my palmy authendication. However, getting an mistake arsenic "CSRF Token Validation failed"
Please counsel wherever I americium doing wrong. Also, I americium capable to station successfully via POSTMAN with aforesaid information and Basic Authentication utilizing POSTMAN GET and POST petition successful the aforesaid investigating client.
My Code:
lv_url = |http://MyURL|. " Create HTTP lawsuit instance CALL METHOD cl_http_client=>create_by_url EXPORTING url = lv_url IMPORTING lawsuit = lo_http_client. * make the URI for the client. l_query = lv_url. CALL METHOD cl_http_utility=>set_request_uri EXPORTING petition = 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'. lo_http_client->authenticate( username = 'username' "iv_username' password = 'password' lawsuit = '900' connection = 'E' ). * acceptable header tract for fetching X-CSRF token CALL METHOD lo_http_client->request->set_header_field EXPORTING sanction = 'X-CSRF-Token' worth = 'Fetch'. 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 sanction = 'X-CSRF-Token' RECEIVING worth = l_token. data : lv_http_status benignant i, lv_status_text benignant string. " Check for palmy response lo_http_client->response->get_status( IMPORTING CODE = lv_http_status " HTTP Status Code REASON = lv_status_text " HTTP presumption description ). * Set X-CSRF- Token successful the caller request. CALL METHOD lo_http_client->request->set_header_field EXPORTING sanction = 'X-CSRF-Token' worth = l_token. *lo_http_client->set_csrf_token( 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'. l_body = '{"MANDT":"900","ID":"0000000002","CUST_NAME":"POST_METHOD","INV_NO":"1234567890"}'. lo_http_client->request->set_cdata( DATA = l_body ). * make Body for the HTTP Post request CALL METHOD lo_http_client->request->set_cdata EXPORTING information = 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 : / L_RESULT.