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

How to handle HTTP authentication using HttpURLConnection?

Do you need output streaming? The HttpURLConnection most definitely supports authentication with the Authenticator class, see: Http Authentication. Update: In case the Authenticator is not an option, you can manually do HTTP basic authentication by adding an extra header to your HTTP request. Try the following code (untested): String userPassword = username + “:” + … Read more

Read error response body in Java

Here is the bug report (close, will not fix, not a bug). Their advice there is to code like this: HttpURLConnection httpConn = (HttpURLConnection)_urlConnection; InputStream _is; if (httpConn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) { _is = httpConn.getInputStream(); } else { /* error from server */ _is = httpConn.getErrorStream(); }

Parse JSON from HttpURLConnection object

You can get raw data using below method. BTW, this pattern is for Java 6. If you are using Java 7 or newer, please consider try-with-resources pattern. public String getJSON(String url, int timeout) { HttpURLConnection c = null; try { URL u = new URL(url); c = (HttpURLConnection) u.openConnection(); c.setRequestMethod(“GET”); c.setRequestProperty(“Content-length”, “0”); c.setUseCaches(false); c.setAllowUserInteraction(false); c.setConnectTimeout(timeout); … Read more

Why do you have to call URLConnection#getInputStream to be able to write out to URLConnection#getOutputStream?

The API for URLConnection and HttpURLConnection are (for better or worse) designed for the user to follow a very specific sequence of events: Set Request Properties (Optional) getOutputStream(), write to the stream, close the stream getInputStream(), read from the stream, close the stream If your request is a POST or PUT, you need the optional … Read more

Android download PDF from URL then open it with a PDF reader

Hi the problem is in FileDownloader class urlConnection.setRequestMethod(“GET”); urlConnection.setDoOutput(true); You need to remove the above two lines and everything will work fine. Please mark the question as answered if it is working as expected. Latest solution for the same problem is updated Android PDF Write / Read using Android 9 (API level 28) Attaching the … Read more