<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Triggering POST for SAP Odata from VBScript : 403 CSRF token validation failed error in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/triggering-post-for-sap-odata-from-vbscript-403-csrf-token-validation/m-p/612781#M25305</link>
    <description>&lt;P&gt;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: &lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;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&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 14 Mar 2018 05:27:24 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2018-03-14T05:27:24Z</dc:date>
    <item>
      <title>Triggering POST for SAP Odata from VBScript : 403 CSRF token validation failed error</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/triggering-post-for-sap-odata-from-vbscript-403-csrf-token-validation/m-p/612781#M25305</link>
      <description>&lt;P&gt;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: &lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;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&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Mar 2018 05:27:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/triggering-post-for-sap-odata-from-vbscript-403-csrf-token-validation/m-p/612781#M25305</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-03-14T05:27:24Z</dc:date>
    </item>
  </channel>
</rss>

