我正在尝试向Cloudant数据库发送HTTP PUT请求,以将密钥“ BPInc”更新为“ N”。当前值为“ Y”。我正在使用HTTPURLConnection进行连接并发送请求。我能够成功发送GET请求并检索BPInc值。但是,当我发送PUT请求时,却收到错误代码-400。
我也研究了cloudant-client库,但是无法获取如何发送PUT请求,我只想坚持使用HTTPURLConnection方法。
这是代码,
// After retrieving _rev and _id using GET request......
String revID = "_rev of the document";
String docID = "_id of the document";
URL postUrl = new URL("<Cloudant URL>/<DB NAME>/" + docID);
String usernameColonPassword = "<API KEY>:<PASSWORD>";
String basicAuthPayload = "Basic " + Base64.getEncoder().encodeToString(usernameColonPassword.getBytes());
HttpURLConnection conn = (HttpURLConnection)postUrl.openConnection();
conn.setRequestMethod("PUT");
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", basicAuthPayload);
conn.setDoOutput(true);
OutputStreamWriter json = new OutputStreamWriter(conn.getOutputStream());
json.write(String.format("{\"_rev\":revID, \"BPInc\":\"Y\"}"));
json.flush();
json.close();
int putResponseCode = conn.getResponseCode();
我对此很陌生,所以我想我可能做错了什么。请提出建议。