I have a web-application that I need to do some API level testing. I was able to make a Django Post request API call in curl command as this:
curl 'https://my-server.com/blablabla/api/public/v1/profiles'
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0'
-H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed
-H 'Referer: https://my-server.com/blablabla/api/public/v1/profiles'
-H 'X-CSRFToken: ...'
-H 'Connection: keep-alive'
-H 'Cookie: csrftoken=...; sessionid=...'
-H 'Content-Type: application/json'
--data-binary '{"group":"1","name":"XYZ"}'
But, if I was trying to port the similar code into python3 as follows:
#!/usr/bin/python3
import requests
import json
TOKEN = 'X-CSRFToken: ...'
COOKIE = 'Cookie: ...; sessionid=...'
headers = {'X-CSRFToken': TOKEN, 'Cookie': COOKIE}
post_data = '{"group":"1","name":"XYZ"}'
response = requests.put("https://my-server.com/blablabla/api/public/v1/profiles", data=post_data, headers=headers)
print(response.json())
print(response.ok)
print(response.status_code)
I have got such failure in return,
{'msg': ['CSRF Failed: CSRF cookie not set.']}
False
403
Does anyone know what could be wrong ?