Python : Trying to POST form using requests

Python : Trying to POST form using requests

You can use the Session object

import requests
headers = {User-Agent: Mozilla/5.0}
payload = {username:niceusername,password:123456}

session = requests.Session()
session.post(https://admin.example.com/login.php,headers=headers,data=payload)
# the session instance holds the cookie. So use it to get/post later.
# e.g. session.get(https://example.com/profile)

Send a POST request with content type = form-data:

import requests
files = {
    username: (None, myusername),
    password: (None, mypassword),
}
response = requests.post(https://example.com/abc, files=files)

Python : Trying to POST form using requests

I was having problems here (i.e. sending form-data whilst uploading a file) until I used the following:

files = {file: (filename, open(filepath, rb), text/xml),
         Content-Disposition: form-data; name=file; filename= + filename + ,
         Content-Type: text/xml}

Thats the input that ended up working for me. In Chrome Dev Tools -> Network tab, I clicked the request I was interested in. In the Headers tab, theres a Form Data section, and it showed both the Content-Disposition and the Content-Type headers being set there.

I did NOT need to set headers in the actual requests.post() command for this to succeed (including them actually caused it to fail)

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *