austenallred austenallred - 1 year ago 82
Python Question

Submit contact form using POST request (requests & python3)

I'm not sure if such a thing is possible, but I am trying to submit to a form such as https://lambdaschool.com/contact using a POST request.

I currently have the following:

import requests

payload = {"name":"MyName","lastname":"MyLast","email":"[email protected]","message":"My message"}

r = requests.post('http://lambdaschool.com/contact',params=payload)

print(r.text)


But I get the following error:

<title>405 Method Not Allowed</title>


etc.

Is such a thing possible to submit using a POST request?

Answer Source

If it were that simple, you'd see a lot of bots attacking every login form ever.

That URL obviously doesn't accept POST requests. That doesn't mean the submit button is POST-ing to that page

You need to open the chrome / Firefox dev tools and watch the request to see what happens on form submit and replicate that data in Python.

Another option would be the mechanize or Selenium webdriver libraries to simulate a browser and fill out the form

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download