Skip to Content

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

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
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

0 Answers