How do I submit contact form using POST request (requests & python3)?

Question:

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 code:

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?

Asked By: austenallred

||

Answers:

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 (though clicking the button also gives that same error…)

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

Answered By: OneCricketeer

params is for query parameters. You either want data, for a form encoded body, or json, for a JSON body.

Answered By: Josh Lee

I think the url should be ‘http://lambdaschool.com/contact-form‘.

Answered By: Emily S.