Skip to Content
1
Jul 28, 2021 at 02:41 PM

CSRF token validation failed in S/4 Hana system.

40485 Views

Hi all.

I am working on a multitenant application based on Cloud application programming model (Node.js). it is to onboard business partners to s/4 Hana system. Connection to s/4 system is using cloud connector and destination. the connection to the system is perfectly working

Destination :-

Issue :-

When i am trying to create a business partner (POST to s/4 system) i am getting "CSRF token validation failed". Most of the solution said that in order to post first we need to fetch the CSRF token. as it is a multitenant application when i use the '@sap-cloud-sdk/core' directly it broke the multitenancy (i even given jwt as a parameter). so what i did was, i created a new api endpont in abap(in s/4 system) that returns the CSRF token

Call to fetch token : -

const s4h = await cds.connect.to('API_CUSTOM');
                const tx = s4h.tx(req);
const csrfToken = await tx.send('GET', 'customrest/getcsrftoken', null, {
                    "X-CSRF-Token": "fetch",
                })

this gives the CSRF Token. After fetching the token i made a post call

const s4h_bp = await cds.connect.to('API_BUSINESS_PARTNER');
const tx_bp = s4h_bp.tx(req);
//BP contains the business partner data to be created
const response = await tx_bp.send('POST', 'A_BusinessPartner', BP, {
                    "X-CSRF-Token": csrfToken,
                })             

on the post call i am getting error as described below.

Any solution to POST this data to S/4 system keeping the multitenancy.

Any help is appreciated.

Thank you ��.

Attachments

dest.png (72.2 kB)
error.png (93.0 kB)