I want to use curl within a java program using Runtime.getRuntime().exec
The full snippet is below yet what my problem boils down to is that I get an error response when I use the curl command in Runtime.getRuntime().exec(command) but when I System.out.println the command, copy and paste it to a shell and execute it there it works fine.
System.out.println(command);
Runtime.getRuntime().exec(command);
What could cause this different outcome between runtime exec and executing the command in the shell.
Thanks for any hints
martin
updates:
The error response I get when using Runtime is: {"error":"unauthorized"}
As I see from the commands that seems to be unclear. I dont get any Exception the curl command runs through but the json response is as posted above.
String APP_ID = "XXX";
String REST_API_KEY = "YYY";
String header = "-H \"X-Parse-Application-Id: " + APP_ID
+ "\" -H \"X-Parse-REST-API-Key: " + REST_API_KEY
+ "\" -H \"Content-Type: application/zip\" ";
public void post2Parse(String fileName) {
String outputString;
String command = "curl -X POST " + header + "--data-binary '@"
+ fileName + "' https://api.parse.com/1/files/" + fileName;
System.out.println(command);
Process curlProc;
try {
curlProc = Runtime.getRuntime().exec(command);
DataInputStream curlIn = new DataInputStream(
curlProc.getInputStream());
while ((outputString = curlIn.readLine()) != null) {
System.out.println(outputString);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}