我正在使用Github的API更新存储库中的文件。follwing命令在curl(替换令牌字段)中起作用:
curl -i -X PUT -H 'Authorization: token 2****************0' -d '{"path": "test6.txt", "message": "test", "sha":"863ba79d293acda68556bbddc1f97a29cb7b98bf","content": "dGVzdDM0Cg==", "
branch": "master"}' https://api.github.com/repos/pedro-roberto/test2/contents/test6.txt
sha从此处更新并获取https://api.github.com/repos/pedro-roberto/test2/contents/test6.txt 。content参数是我要用于更新的文件的base64编码(在命令行中base64 file.txt,在Java中byte[] encodedBytes = Base64.encodeBase64(JsonArray_TO_UPLOAD.toString().getBytes()); String jsonInBase64 = new String(encodedBytes);)
我试图用Java发出此请求,但失败,并显示响应代码 400:Bad request
尝试1
Map<String, String> parameters = new HashMap<>();
parameters.put("path", "test6.txt" );
parameters.put("message","test");
parameters.put("sha", "863ba79d293acda68556bbddc1f97a29cb7b98bf" );
parameters.put("content", "dGVzdDM0Cg==");
parameters.put("branch", "master");
URL url = new URL(https://api.github.com/repos/pedro-roberto/test2/contents/test6.txt);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("PUT");
con.setRequestProperty("Authorization","token 2********************0");
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();
相关分类