简体   繁体   中英

Successful curl request fails with Java HttpURLConnection and Apache HttpClient

I am using Github's API to update a file in a repository. The follwing command works in curl (token field replaced):

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

The sha is updated and obtained from here https://api.github.com/repos/pedro-roberto/test2/contents/test6.txt The content parameter is the base64 encoding of the file I want to use to update (in the command line base64 file.txt , in Java byte[] encodedBytes = Base64.encodeBase64(JsonArray_TO_UPLOAD.toString().getBytes()); String jsonInBase64 = new String(encodedBytes); )

I have tried to do this request in Java but I fail with a response code 400:Bad request

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

Attempt 2

List<NameValuePair> params = new ArrayList<NameValuePair>();;
params.add(new BasicNameValuePair("path", "test6.txt");
params.add(new BasicNameValuePair("message","test"));
params.add(new BasicNameValuePair("sha", "863ba79d293acda68556bbddc1f97a29cb7b98bf"));
params.add(new BasicNameValuePair("content", "dGVzdDM0Cg=="));
params.add(new BasicNameValuePair("branch", "master"));
HttpClient httpclient = HttpClients.createDefault();
HttpPut con2 = new HttpPut(link);
con2.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
con2.addHeader("Authorization","token 2*****************0");
HttpResponse response = httpclient.execute(con2);
HttpEntity entity = response.getEntity();

Don't know what to do to make this work. Please help!

EDIT: I think the problem has to do with the way I build the query string. I was trying to make it work in Postman and it only worked when I stopped using parameters and used body/raw/JSON and sent the parameters as JSON (issue here: 400 Problems parsing JSON - GitHub API and POSTMAN using OAuth ). Don't know how to integrate this in Java though...

After many trials and errors, I managed to find a solution:

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");
HttpClient httpclient = HttpClients.createDefault();
HttpPut con2 = new HttpPut(https://api.github.com/repos/pedro-roberto/test2/contents/test6.txt);
con2.addHeader("Authorization","token 2******************0");

//tricky part
con2.addHeader("Content-Type", "application/x-www-form-urlencoded");
Gson gson = new Gson();
String json = gson.toJson(parameters);
con2.setEntity(new StringEntity(json));

HttpResponse response = httpclient.execute(con2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM