How to send Request payload to REST API in java?
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”); … Read more