对 Bitbucket API 的 HTTP PUT 请求

发布于 2024-11-16 18:24:16 字数 1168 浏览 0 评论 0原文

我想开发一个用于 bitbucket 问题 API 访问的 java 库。
我已经问过一个关于HTTP Content-Length 标头的计算的问题,但这个问题具体是关于 Bitbucket API 以及更新问题的过程(因为所有其他请求都运行良好)。

以下代码不起作用,会出现 411 Length required 错误。
但更令人困惑的是:在文档中,您被告知使用 PUT 请求方法。如果您“忘记”指定,状态代码将更改为 200 OK,但问题保持不变。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2024-11-23 18:24:16

我更新的代码示例在 stackoverflow 上的另一个问题的帮助下有效: 如何在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求?看起来不起作用

利用连接的OutputStream 可以工作。

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

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文