'Upload file to Django in Requests.POST
Trying to upload a text file (of molecules) to ADMETlab 2.0 and download the corresponding CSV output, from within python.
Tried the following code to check whether the output is returned properly:
import requests
fyle = 'infile.sdf'
Headers = {"Referer" : "https://admetmesh.scbdd.com/service/screening/index", "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"}
with open(fyle, 'rb') as f:
r = requests.post('https://admetmesh.scbdd.com/service/screening/cal', files={fyle: f}, headers=Headers)
print(r.text)
Which led to this error:
ValueError: invalid literal for int() with base 16: b
I tried this fix and now, no error is raised but nothing except a blank new line is printed. Where am I going wrong?
A try with cookies:
import requests
fyle = 'infile.sdf'
Headers2 = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
,'Accept-Encoding': 'gzip, deflate, br'
,'Accept-Language': 'en-US,en;q=0.9,bn;q=0.8'
,'Cache-Control': 'max-age=0'
,'Connection': 'keep-alive'
,'DNT': '1'
,'Host': 'admetmesh.scbdd.com'
,'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"'
,'sec-ch-ua-mobile': '?0'
,'sec-ch-ua-platform': '"Windows"'
,'Sec-Fetch-Dest': 'document'
,'Sec-Fetch-Mode': 'navigate'
,'Sec-Fetch-Site': 'same-origin'
,'Sec-Fetch-User': '?1'
,'Upgrade-Insecure-Requests': '1'
,'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}
r1 = requests.get('https://admetmesh.scbdd.com/service/screening/cal', headers=Headers2)
print(r1.cookies['csrftoken'])
Headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
,'Accept-Encoding': 'gzip, deflate, br'
,'Accept-Language': 'en-US,en;q=0.9,bn;q=0.8'
,'Cache-Control': 'max-age=0'
,'Connection': 'keep-alive'
,'Content-Length': '12446'
,'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryKxO79kdb1BmzFSEV'
,'Cookie': 'csrftoken='+str(r1.cookies['csrftoken'])
,'DNT': '1'
,'Host': 'admetmesh.scbdd.com'
,'Origin': 'https://admetmesh.scbdd.com'
,'Referer': 'https://admetmesh.scbdd.com/service/screening/cal'
,'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"'
,'sec-ch-ua-mobile': '?0'
,'sec-ch-ua-platform': '"Windows"'
,'Sec-Fetch-Dest': 'document'
,'Sec-Fetch-Mode': 'navigate'
,'Sec-Fetch-Site': 'same-origin'
,'Sec-Fetch-User': '?1'
,'Upgrade-Insecure-Requests': '1'
,'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}
with open(fyle, 'rb') as f:
r = requests.post('https://admetmesh.scbdd.com/service/screening/cal', files={fyle: f}, headers=Headers)
print(r.cookies)
print(r.text)
r1 returns a cookie but r does not and r.text gives out a CSRF error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
