HTTP PUT Request to Bitbucket API
I want to develop a java library for bitbucket issues API access.
I've already asked a question about the computation of the HTTP Content-Length header, but this question is specifically about the Bitbucket API and the process of updating an issue (since every other request works well).The following code doesn't work, giving a 411 Length Required
error.
PUT
request method. If you "forget" to specify that, status code changes to 200 OK
, but leaving the issue unchanged.
public class PutTest {
public static void main(String[] args) throws Exception {
URL u = new URL("https://api.bitbucket.org/1.0/repositories/myname/myproject/issues/1/?title=hello+world");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.addRequestProperty("Authorization", "Basic "+Base64.encodeToString("user:password".getBytes(), false));
c.addRequestProperty("Content-Length", String.valueOf(u.getQuery().getBytes("UTF-8").length));
c.setRequestMethod("PUT");
c.connect();
System.out.println(c.getResponseCode()+" "+c.getResponseMessage());
}
}
My updated code sample works, with help of another question at stackoverflow: How to send PUT, DELETE HTTP request in HttpURLConnection? Looks like not working.
Utilizing connection's OutputStream
works.
public class PutTest {
public static void main(String[] args) throws Exception {
URL u = new URL("https://api.bitbucket.org/1.0/repositories/myname/myproject/issues/1/");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.addRequestProperty("Authorization", "Basic "+Base64.encodeToString(("user:password").getBytes(), false));
c.setRequestMethod("PUT");
c.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());
out.write("title=hello+world");
out.close();
c.connect();
System.out.println(c.getResponseCode()+" "+c.getResponseMessage());
}
}
精彩评论