We have a locally installed BitBucket server.
Im trying to do a PUT from a Java application with a new file.
Executing this curl code in the console works when I have a README.txt file in the same path, so I thought I just translate that into Java.
curl -X PUT -u user:pass -F content=@README.txt -F message=curl -F branch=master http://bitbucket.abc.com:7990/rest/api/1.0/projects/ABCD/repos/mystuff/browse/README.txt
My Java implementation looks like this:
try {
String url = "http://bitbucket.abc.com:7990/rest/api/1.0/projects/ABCD/repos/mystuff/myfile.txt";
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("PUT");
con.setDoOutput(true);
con.setRequestProperty("branch","master");
con.setRequestProperty("message","just a message");
con.setRequestProperty("content","@myfile.txt");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
String user = "user";
String pw = "pass";
String encoded = Base64.getEncoder().encodeToString((user + ":" + pw).getBytes(StandardCharsets.UTF_8));
con.setRequestProperty("Authorization", "Basic " + encoded);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
writer.write("Write some stuff in the file that will be persisted in the file.");
writer.flush();
writer.close();
con.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
I don’t get any exception and the file does not end up in the BitBucket repository.
Can anyone see any obvious error in the code?
/Bill