Search code examples
python-requestshttp-postx-xsrf-token

How to get bond data from fundsupermart using post method?


I am trying to get bond data from this page, "https://secure.fundsupermart.com/fsm/bonds/factsheet/XS1415758991/DEGREE-7-250-03Jun2021-Corp-USD".

I inspected the page, the data can be found in Network-XHR, name is XS1415758991. The name is the bond ISIN code. I then found the link behind the name. “https://secure.fundsupermart.com/fsm/rest/bond-info/get-bond-factsheet-data/XS1415758991”. In the Headers, I also found "x-xsrf-token: 38ccccd8-6cae-46a9-b916-2e80e607e107".

import requests
url_bond = r'https://secure.fundsupermart.com/fsm/rest/bond-info/get-bond-factsheet-data/XS1415758991'
headers = {'Accept-Encoding': 'gzip', }
data = {"x-xsrf-token":'38ccccd8-6cae-46a9-b916-2e80e607e107'}
r = requests.post(url_bond,headers=headers, data = data)
r.text

I tried following codes, but replied "'Expected CSRF token not found. Has your session expired?'". I expect to get the data in ","bondPriceJsonHashmap"--"THREE_YEAR"


Solution

  • I solved the question myself. The solution is simple. 1) find the token in cookies 2) post to the url.

    URL = 'https://secure.fundsupermart.com/fsm/rest/bond-info/get-bond-factsheet-data/JK8897874'
    client = requests.session()
    client.get(URL)  # sets cookie
    csrftoken = client.cookies['XSRF-TOKEN']
    headers = {'x-xsrf-token':csrftoken}
    r = client.post(URL, headers= headers)
    r.content