Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Triggering POST for SAP Odata from VBScript : 403 CSRF token validation failed error

Former Member
0 Kudos

I'm trying to trigger POST request for SAP Odata from VBScript. But i'm gettting the error 'CSRF token validation failed'. I've done the following improvements: I am able to get CSRF token first by triggering a GET request and then using the same for POST. But still error is there. I've tested the Odata in POSTMAN where it is working correctly with content-type header value as: application/json. But it doesn't work in VB. Can you please help me for the same. Below is the code:

Dim ws As Worksheet: Set ws = Worksheets("Sheet1")

 strUrl = "[URL WITH ENTITY SET NAME, like, _____SRV/EntitySetName]"
 hReq.Open "GET", strUrl, False
 hReq.SetCredentials "[USERNAME]", "[PASSWORD]", HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
 hReq.SetRequestHeader "X-CSRF-Token", "Fetch"

 hReq.Send

 hResp = hReq.ResponseText

 Dim csrfToken As String
 Dim xCookie As String
 Dim xWith As String

 csrfToken = hReq.GetResponseHeader("x-csrf-token")

 If Not xDoc.LoadXML(hResp) Then
    MsgBox "Load fail"
    MsgBox hResp
 Else
    Set hReq = Nothing

    hReq.Open "POST", strUrl, False
    hReq.SetCredentials "[USERNAME]", "[PASSWORD]", HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
    hReq.SetRequestHeader "Content-Type", "application/json" 
    hReq.SetRequestHeader "x-csrf-token", csrfToken

    hReq.Send ("{""Id"":167,""Name"":""AAA"",""Age"":20,""Desig"":""BBB"",""Doj"":""2017-10-16T00:00:00""}")

    hResp = hReq.ResponseText ' HERE ERROR IS CSRF TOKEN VALIDATION FAILED
 End If


 Set xDoc = Nothing
 Set hReq = Nothing
  • SAP Managed Tags:
0 REPLIES 0