0

I'm trying to use the python 'requests' lib to submit an https post. however, i keep running into this error "CSRF verification failed. Request aborted"

I have reviewed all similar issues reported on the forum but unable to find a solution.

Below is my python code. Appreciate any help.

import requests

payload = {
    'targetip': 'www.ndtv.com'
    }
header = {
    'User-Agent': 'python-requests/2.20.1', 
    'Accept-Encoding': 'gzip, deflate', 
    'Accept': '*/*', 
    'Connection': 'keep-alive',
    'Referer':'https://dnsdumpster.com/'
    }

url = r'https://dnsdumpster.com/'

with requests.session() as s:
    res = s.get(url)
    cookie = s.cookies
    res = requests.post(url, data=payload, headers = header, cookies = cookie)
    print(res.text)
A_K
  • 81
  • 2
  • 10
  • If you watch the post request from actually sending a request from that site, the payload contains both `targetip` and `csrfmiddlewaretoken`. You'll have to generate that token and add it to your payload. – Adam Smith Dec 02 '18 at 05:20
  • https://stackoverflow.com/questions/13567507/passing-csrftoken-with-python-requests Please refer to this, this might help – shahidammer Dec 02 '18 at 05:21
  • Thanks. adding the 'csrfmiddlewaretoken' works well – A_K Dec 02 '18 at 05:35

1 Answers1

0

it missing request body csrfmiddlewaretoken, you can get it in cookies

with requests.session() as s:
    res = s.get(url)
    payload['csrfmiddlewaretoken'] = s.cookies['csrftoken']
    res = requests.post(url, data=payload, headers = header, cookies = s.cookies)
    print(res.text)
ewwink
  • 18,382
  • 2
  • 44
  • 54