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:
- javascript – NodeJS express Post form – action always 404
- Website Facebook share show in carousel post format
- Upload Image using POST form data in Python-requests
- c1 cms – Post form values, process with c# in Composite C1
- javascript – React, how to POST form data to an API endpoint?
- javascript – Angular4 – post form data to rest api
- Cannot POST form node.js – express
- php – Customizing WordPress Gallery Post Format
- Default Django Rest Framework HTML POST Forms not showing after upgrading to latest versions
- Can I post formatted Source Code on Facebook?