29

I want to retrieve the JSON data from the following: https://git.eclipse.org/r/#/c/11376/

Request URL: https://git.eclipse.org/r/gerrit/rpc/ChangeDetailService

Request Method: POST

Request Headers:

Accept:application/json

Content-Type:application/json; charset=UTF-8

Request Payload:

{"jsonrpc":"2.0","method":"changeDetail","params":[{"id":11376}],"id":1}

I already tried this answer but I am getting 400 BAD REQUEST.

Can anyone help me sort this out?

Thanks.

Community
  • 1
  • 1
Gangaraju
  • 4,406
  • 9
  • 45
  • 77

2 Answers2

41

The following code works for me.

//escape the double quotes in json string
String payload="{\"jsonrpc\":\"2.0\",\"method\":\"changeDetail\",\"params\":[{\"id\":11376}],\"id\":2}";
String requestUrl="https://git.eclipse.org/r/gerrit/rpc/ChangeDetailService";
sendPostRequest(requestUrl, payload);

method implementation:

public static String sendPostRequest(String requestUrl, String payload) {
    try {
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        writer.write(payload);
        writer.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuffer jsonString = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
                jsonString.append(line);
        }
        br.close();
        connection.disconnect();
        return jsonString.toString();
    } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
    }

}
alex440
  • 1,647
  • 3
  • 20
  • 35
Gangaraju
  • 4,406
  • 9
  • 45
  • 77
  • 2
    BufferedWriter is slightly overkill here. Moreover, you will have troubles if you send UTF-8 characters. You should use `OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");` instead. – Aurélien Bénel Mar 24 '13 at 08:10
  • As the content of a `String` cannot be modified (only the reference can), concatenating strings over and over will create bigger and bigger objects. You should use `StringBuffer` instead. https://github.com/Hypertopic/Porphyry/blob/master/src/org/hypertopic/RESTDatabase.java#L221 – Aurélien Bénel Mar 24 '13 at 08:33
  • What if the url provides a xm format? How would you do this? – JayC Jan 25 '17 at 19:54
  • The doInput flag is true by default so you don't need it to set it o true. – coconut Jul 25 '18 at 11:05
1

I tried with a rest client.

Headers :

  • POST /r/gerrit/rpc/ChangeDetailService HTTP/1.1
  • Host: git.eclipse.org
  • User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0
  • Accept: application/json
  • Accept-Language: null
  • Accept-Encoding: gzip,deflate,sdch
  • accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  • Content-Type: application/json; charset=UTF-8
  • Content-Length: 73
  • Connection: keep-alive

it works fine. I retrieve 200 OK with a good body.

Why do you set a status code in your request? and multiple declaration "Accept" with Accept:application/json,application/json,application/jsonrequest. just a statement is enough.

Calden
  • 76
  • 5