2

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.
But even more confusing: In the documentation, you are told to use 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());
    }
}
Community
  • 1
  • 1
f4lco
  • 3,728
  • 5
  • 28
  • 53
  • I haven't read their API docs, but presumably a GET to that URL returns a representation of the issue, without changing it. It's not an error to make a GET request to that URL so you get a "200 OK" response. – Tyler Jun 23 '11 at 17:15

1 Answers1

1

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());
    }
}
Community
  • 1
  • 1
f4lco
  • 3,728
  • 5
  • 28
  • 53
  • Did you finish the library? I was about to tackle the same thing when I found this. – Zulaxia Dec 28 '12 at 14:28
  • I actually did and wouldn't mind sharing. It's lightweight - the only external dependency is Jackson for parsing JSON. I just got it working again - since a year ago Bitbucket has added a handful of fields to their responses. Keep in mind there are plenty of other options available which might include other aspects than my tiny project. If you still want to have a look at it, just add another comment. – f4lco Dec 29 '12 at 14:21
  • I'd love to take a look, sure. I've just found a few other options, but I really need such a small subset (mainly list, new and edit issue), that there's no reason a more feature complete project is going to be the best for me. – Zulaxia Dec 29 '12 at 20:38
  • It's uploaded to [Bitbucket](https://bitbucket.org/phineas/bitbucket-issues-java). Feel free to contribute and comment! I'd especially like to here about the application you build. – f4lco Dec 30 '12 at 14:09