对 Bitbucket API 的 HTTP PUT 请求
我想开发一个用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更新的代码示例在 stackoverflow 上的另一个问题的帮助下有效: 如何在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求?看起来不起作用。
利用连接的
OutputStream
可以工作。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.