I had the same issue. You need to handle the csrf token AND the cookie:
1) Fetch the token and save it, save the cookie as well
headers = {
'content-type': "application/json",
'accept': "application/json",
'authorization': encoded_auth_token,
'cache-control': "no-cache",
'X-CSRF-TOKEN': "fetch"
}
response = requests.request("GET", url, headers = headers)
my_token = response.headers['X-CSRF-Token']
my_cookie = response.cookies
2) Then do your POST API calls with the saved csrf token and the cookie:
cookies = my_cookie
headers = {
'content-type': "application/json",
'accept': "application/json",
'authorization': encoded_auth_token,
'cache-control': "no-cache",
'X-CSRF-TOKEN': my_token
}
payload = {}
response = requests.request("POST", url, headers = headers, data=json.dumps(payload), cookies=cookies)
print(response.content)
Voila.
Hope it helps.