function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NikeNike 

SAP-SFDC Integration

Am getting the following error in POST http method.

 

Error: CSRF token validation failed and System.HttpResponse[Status=Forbidden, StatusCode=403]"|0x43de18c1

 

I have two http request, 1. GET and 2. POST. Please review my code below and let me know where am missing.

 

GET:

    Http h = new Http();
    // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type', 'application/atom+xml');    
    req.setHeader('x-csrf-token', 'fetch');
    req.setHeader('Authorization', 'BASIC ' + EncodingUtil.base64Encode(Blob.valueOf('username:pasword')));
    req.setMethod('GET');
    req.setEndpoint(url1);
    HttpResponse res = h.send(req);


POST: Using the above response and passing the CSRF token to my POST method,


        req.setHeader('Content-Type', 'application/atom+xml');
        req.setHeader('X-CSRF-Token',maptoken.get('x-csrf-token'));   
        req.setHeader('Authorization', 'BASIC ' + EncodingUtil.base64Encode(Blob.valueOf('username:pasword')));
        req.setMethod('POST');
        req.setEndpoint(url2);     
        req.setBody('xml hard coded');
        HttpResponse ires = h.send(req);

NikeNike

I have fixed it. Set the cookie in the header will solve the issue.

Tony TuanTony Tuan
I'm facing the same issue. What is the cookie should be like?
EmadEmad
Can you elaborate Set the cookie in the header will solve the issue

I used this in GET:
strCookies = res.getHeader('Set-Cookie')

and in SET:
req2.setHeader('Cookie', strCookies);

But still getting the CSRF token validation failed message.
 
hari.phari.p
Hi We are facing the same issue.Any one please let me know how you solved this by setting cookie? Thanks 
Salesforce The SizeSalesforce The Size
Can someone solved this problem?