How to POST Python requests with CSRF token ?


import requests
client = requests.session()

# Retrieve the CSRF token first
URL = 'http://www.example.com/'
client.get(URL) # sets cookie
csrftoken = client.cookies['csrftoken']
print csrftoken #Check if it's getting printed. Sometimes key name could be 'csrf' only

payload = {
'csrfmiddlewaretoken':csrftoken,
'key1':'value1',
'key2':'value2',
'key3':'value3'
}

r = client.post(URL, data=payload, headers=dict(Referer=URL))