Skip to Content

CSRF token validation failed","status_code":403 error in CAI

Hi,

I am using a get method for Odata and I am storing the CSRF token in a variable called token which i got it form the get method and passing the CSRF token in post method but getting the error : CSRF token validation failed","status_code":403

is there anything that i am missing to add in the headers ?

can you help me

Thanks
Kishore

csrftoken.jpg (91.0 kB)
Add a comment
10|10000 characters needed characters exceeded

  • Hi Thomas Bruckner,

    Yes i checked it by posting it on the chat by doing {{memory.token}} and i can see the token,

    but the post API is not working, I tried it in using Angular 9 app and the error i am getting there is : Request header field x-csrf-token is not allowed by Access-Control-Allow-Headers in preflight response.

    in both the cases i could able to get the csrf token but the post is not working

    can you help me here


    Thanks
    kishore

  • Hi Thomas Bruckner,

    the post API is working fine if i do the post API call in postman Application, i am passing the

    data:

    POST /sap/c4c/odata/v1/ticket/ServiceRequestCollection HTTP/1.1 
    Host: ************.com
    x-csrf-token: ******************
    Authorization: Basic ***********bTpXZWxjb21lbA==
    Content-Type: application/json Cache-Control: no-cache
    Postman-Token: ***********-bb9c-1bcc0bb5767e
    { "Name" : "Postman Test-ticket" }

    and In my angular app in headers I am sending

    .set('Authorization', 'Basic ********mdhdXRhbTpXZWxjb21lbA==')
     .set('x-csrf-token', 'fetch')
     .set('Access-Control-Allow-Headers', 'X-CSRF-Token, Content-Type')
    

    I am missing anything in the headers section in CAI or Angular app?

    I am not sure why the post is working in Postman and not in my CAI or Angular app
    can you suggest me here


    Thanks
    kishore

  • Could you provide the exact error message receieved, like both status, body & headers?
    It sounds it is coming from the C4C Backend / API and indicating there is something wrong in the constructed call.
    (Unfortunately I am not an expert for C4C, but I would say it generally helps).

Related questions

2 Answers

  • Posted on Aug 20 at 05:05 PM

    Hi,

    CSRF token is always generated against a session ID/cookie. So when you make a GET call to C4C with x-csrf-token=fetch as request header than in the response along with csrf token you also get some session cookies.

    response-header.png

    So when making the POST call from rest clients like POSTMAN cookies are already shared and hence it is successful. Whereas when making the same call with SAP CAI it fails because CAI does not pass the session context to C4C there C4C does not know against which session your CSRF needs to be evaluated.

    Now in order to fix this problem, you also need to pass "Cookie" as a request header parameter for your POST call along with CSRF value. This "Cookie" header will contain the value of all the response cookies from response of GET request.

    Then it should work.

    BTW.. You can also see in POSTMAN via code generation how they sent the value of response cookie with CXRF Token you passed. header-sent-from-postman.png

    Thanks

    Saurabh


    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 14 at 01:22 PM

    Hi Siva,

    One suggestion to possibly correct the issue would be to set up a destination in your Cloud Foundry account for the SAP system.

    You can then refer to the system as:

    destination://<destination name>/sap/opu/odata/sap/<OData service>

    Do let me know if this works for you.

    Best Regards,

    Rohit

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.