I am trying to emulate this java code
public static void uploadFile(String filename, String systemID){
try{
String createNew = "false";
//check for backup files to know if we should make a new file on the server
File f = new File(filename + ".1");
if(f.exists()){
createNew = "true";
f.delete();
}
HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL(uploadURL).openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Content-encoding", "deflate");
httpUrlConnection.setRequestProperty("Content-type", "application/octet-stream");
java.io.OutputStream os = httpUrlConnection.getOutputStream();
Thread.sleep(1000);
String fileData = IOUtils.toString(new FileReader(filename));
String request = "filedata=" + fileData + "&filename=" + filename + "&systemid=" + systemID + "&createNew=" + createNew;
DeflaterOutputStream deflate = new DeflaterOutputStream(os);
deflate.write(request.getBytes());
deflate.flush();
deflate.close();
os.close();
BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
String s = null;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
in.close();
//fis.close();
}catch(Exception e){
e.printStackTrace();
}
static String uploadURL = "http://vp-creations.com/utilities/fileupload.php";
in python using requests, I think im missing something simple because I havent been able to get the right response from the server
import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}
headers = {'Content-encoding': 'deflate', 'Content-type': 'application/octet-stream'}
payload = {'filedata=': 'foo', 'filename=': 'bar', 'systemid=' : 'fooe', 'createNew=' : 'false'}
r = requests.post(url, files=files, headers=headers, data=payload)
the response from the server that I keep getting is {"response":"error","comment":"missing at least one parameter"}' any help please?
When you pass a dictionary to the data
parameter of requests.post
, it will be form-encoded. I'm not totally familiar with the Java code, but it looks like it is actually using an encoded String
as the data of the request. In order to achieve the same thing with requests
, pass a string as the data
parameter, like so:
data = 'filedata=foo&filename=bar&systemid=&fooe&createNew=false'
r = requests.post(url, files=files, headers=headers, data=data)
See a similar example in the documentation here .
You need to try changing the following things.
Firstly, data
sends the given dictionary in the POST body. If that is genuinely where you want it, you need to remove the '=' elements from the dictionary:
payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}
Secondly, you're adding a pair of incorrect headers. When you use Requests' file
parameter, Requests prepares the body data (including your file) as multipart/form-data'
. You then override that Content-Type with your application/octet-stream
, which is not the form of the body. Additionally, you are claiming that your body data is compressed using deflate
, which it is not.
Try the following code:
import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}
payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}
r = requests.post(url, files=files, data=payload)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.