开发者

HTTP PUT Request to Bitbucket API

开发者 https://www.devze.com 2023-03-15 04:02 出处:网络
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 Bi

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 th开发者_如何转开发e 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());
    }
}


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());
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号