Error CSRF Token Validation Failed in POST API

1 month ago 10

Hi Experts,

I americium getting mistake "CSRF Token Validation Failed" successful POST API. 

I americium capable to make CSRF token successfully done beneath code. But seems token is not getting acceptable hence getting the supra error. 

Please suggest wherever I americium doing wrong. Also, I americium capable to station successfully via POSTMAN with aforesaid information and Basic Authentication.

Many Thanks

DATA(lv_string) = cl_web_http_utility=>encode_base64( lv_Str ).

lv_string = |Basic { lv_string }|.

** Set up header fields for getting X_CSRF-TOKEN

DATA(lo_request2) = lo_http_client->get_http_request( ).

lo_request2->set_header_fields( VALUE #(

( name = 'Content-Type' value = 'application/json' )

( name = 'X-CSRF-Token' value = 'fetch' )

( name = 'Authorization' value = lv_string )

) ).

CONCATENATE i_url1 '/sap/opu/odata/sap/API_MAINTNOTIFICATION/MAINTENANCENOTIFICATION' INTO i_url2.

lo_request2->set_uri_path( i_uri_path = i_url2 ).

DATA(lv_response2) = lo_http_client->execute( i_method = if_web_http_client=>get )->get_header_fields( ).

READ TABLE lv_response2 INTO DATA(ls_repsonse2) WITH KEY name = 'x-csrf-token'.

IF sy-subrc = 0.

DATA(lv_token) = ls_repsonse2-value.

CLEAR ls_repsonse2.

ENDIF.

IF lv_token IS NOT INITIAL.

** Set CSRF Token

lo_http_client->set_csrf_token( ).

" Navigate to the assets and make a petition for the make operation

lo_request = lo_client_proxy->create_resource_for_entity_set( 'MAINTENANCE_NOTIFICATION' )->create_request_for_create( ).

ENDIF.

Read Entire Article