3 weeks ago
Hi Experts,
I am getting error "CSRF Token Validation Failed" in POST API.
I am able to generate CSRF token successfully through below code. But seems token is not getting set hence getting the above error.
Please suggest where I am doing wrong. Also, I am able to post successfully via POSTMAN with same data 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 resource and create a request for the create operation
lo_request = lo_client_proxy->create_resource_for_entity_set( 'MAINTENANCE_NOTIFICATION' )->create_request_for_create( ).
ENDIF.
Hi Sandra,
I am fetching csrf token successfully. Could you please guide me where I am missing to pass token to lo_http_client->set_csrf_token( ).
Thanks in advance!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
102 | |
8 | |
6 | |
6 | |
5 | |
5 | |
4 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.