<?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>Question Re: CSRF Token Fetching for Python Client - OData Services in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaa-p/12715971#M4771870</link>
    <description>&lt;P&gt;Hi Punit,&lt;/P&gt;&lt;P&gt;Please pass this header to get the 'x-csrf-token' in response header. I am attaching the code snippet below for your reference.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anim &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;## CSRF TOken Fetch###
csrf_sess = requests.session()
csrf_sess.headers.update({'connection':'keep-alive'})
header = {
    'x-csrf-token':'fetch',
    'Authorization':f'Basic {base64.b64encode(f"{username}:{password}".encode()).decode()}',
    'Content-Type':'application/json'
}
csrf_url = f"{base_url}/sap/c4c/odata/v1/c4codataapi/ServiceRequestCollection"
csrf_params = {'x-csrf-token':'fetch'}
csrf_call = csrf_sess.get(csrf_url,params=csrf_params,headers=header)

token_header = csrf_call.headers
csrf_token = token_header['x-csrf-token']
print(token_header)
print(token_header['x-csrf-token'])

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Aug 2023 04:04:10 GMT</pubDate>
    <dc:creator>former_member876434</dc:creator>
    <dc:date>2023-08-29T04:04:10Z</dc:date>
    <item>
      <title>CSRF Token Fetching for Python Client - OData Services</title>
      <link>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaq-p/12715968</link>
      <description>&lt;P&gt;I am trying to use Python requests library to fetch the csrf token for an OData service using GET request (code stub below). However the response header doesnt have any flag corresponding to csrf token value.&lt;/P&gt;
  &lt;P&gt;This would subsequently be used for doing a POST on one of the tables but the token validation fails.&lt;/P&gt;
  &lt;P&gt;I tried using Postman and it successfully generates csrf token in response header.&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;&lt;U&gt;OUTPUT&lt;/U&gt;:&lt;/STRONG&gt;&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Cookies&lt;/U&gt;&lt;/STRONG&gt;&amp;lt;RequestsCookieJar[&amp;lt;Cookie sap-usercontext=sap-client=3xx for xx.de/&amp;gt;, &amp;lt;Cookie SAP_SESSIONID_xx_3xx=mBhtGO6hrFCr4PZ6iuvvWp11712FTxHtuZRFfWVfZdw%3d for xx.de/&amp;gt;]&amp;gt;&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Response Header&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
  &lt;P&gt;{'set-cookie': 'sap-usercontext=sap-client=3xx; path=/, SAP_SESSIONID_xx_3xx=VdUUsfqf19sYMFW3jmRDLWajmUWFSRHtttlFfWVfZdw%3d; path=/; HttpOnly', 'content-type': 'application/xml', 'content-length': '889', 'dataserviceversion': '1.0', 'sap-server': 'true', 'sap-perf-fesrec': '74990.000000'}&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Session Header&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
  &lt;P&gt;{'User-Agent': 'python-requests/2.28.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;import requests&lt;BR /&gt;import json&lt;BR /&gt;url="http://url/sap/opu/odata/sap/Service/EntitySet/$format=xml"&lt;BR /&gt;&lt;BR /&gt;sess = requests.session()&lt;BR /&gt;sess.headers.update({'Connection': 'keep-alive'})&lt;BR /&gt;params= { 'x-csrf-token': 'Fetch' }&lt;BR /&gt;r = sess.get(url,auth=(uname,pass),params=params)&lt;BR /&gt;token = r.headers&lt;BR /&gt;print(r.cookies)&lt;BR /&gt;print(token)&lt;BR /&gt;print(sess.headers)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Dec 2022 19:16:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaq-p/12715968</guid>
      <dc:creator>punitsigma47</dc:creator>
      <dc:date>2022-12-26T19:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: CSRF Token Fetching for Python Client - OData Services</title>
      <link>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaa-p/12715969#M4771868</link>
      <description>&lt;P&gt;You'd better post in a Python forum (Stack Overflow, etc.) rather than in an ABAP forum... (your primary tag used).&lt;/P&gt;&lt;P&gt;For sure, you don't send the same HTTP requests than with Postman, but I'm not Python expert.&lt;/P&gt;&lt;P&gt;Don't you have 2 requests with Postman, first one to login, second one to get the x-csrf-token?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2022 09:57:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaa-p/12715969#M4771868</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2022-12-27T09:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: CSRF Token Fetching for Python Client - OData Services</title>
      <link>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaa-p/12715970#M4771869</link>
      <description>&lt;P&gt;The authentication and token response can be achieved in a single request. I have tagged it to ABAP Dev to see if there's any option to disable the csrf validation through ABAP code on server (which is not ideal but it's a confined environment so I would not worry much as of now).&lt;/P&gt;&lt;P&gt;This has worked very well for me in other HTTP servers where the csrf token comes in via header, cookies or hidden in response body. Therefore, I didnt post it to other community websites but I can try.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Dec 2022 10:33:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaa-p/12715970#M4771869</guid>
      <dc:creator>punitsigma47</dc:creator>
      <dc:date>2022-12-27T10:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: CSRF Token Fetching for Python Client - OData Services</title>
      <link>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaa-p/12715971#M4771870</link>
      <description>&lt;P&gt;Hi Punit,&lt;/P&gt;&lt;P&gt;Please pass this header to get the 'x-csrf-token' in response header. I am attaching the code snippet below for your reference.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Anim &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;## CSRF TOken Fetch###
csrf_sess = requests.session()
csrf_sess.headers.update({'connection':'keep-alive'})
header = {
    'x-csrf-token':'fetch',
    'Authorization':f'Basic {base64.b64encode(f"{username}:{password}".encode()).decode()}',
    'Content-Type':'application/json'
}
csrf_url = f"{base_url}/sap/c4c/odata/v1/c4codataapi/ServiceRequestCollection"
csrf_params = {'x-csrf-token':'fetch'}
csrf_call = csrf_sess.get(csrf_url,params=csrf_params,headers=header)

token_header = csrf_call.headers
csrf_token = token_header['x-csrf-token']
print(token_header)
print(token_header['x-csrf-token'])

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Aug 2023 04:04:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/csrf-token-fetching-for-python-client-odata-services/qaa-p/12715971#M4771870</guid>
      <dc:creator>former_member876434</dc:creator>
      <dc:date>2023-08-29T04:04:10Z</dc:date>
    </item>
  </channel>
</rss>

