I am writing to an HTTP POST endpoint using code like this.
When the response code is 200 everything is OK.
When the response code is 400 though I cannot get InputStream
and read the response.
I am just getting an exception on the line marked with /// error !!!
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: ...
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1924)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520)
at com.test123.Main009.main(Main009.java:41)
So is there a way to read the response body even though the response code is 4xx ?
package com.test123;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Main009 {
public static void main(String[] args) throws Exception {
String username = "xxx";
String password = "yyy";
URL url = new URL("http://host/path");
URLConnection uc = url.openConnection();
HttpURLConnection http = (HttpURLConnection)uc;
http.setRequestMethod("POST"); // PUT is another valid option
http.setDoOutput(true);
http.setDoInput(true);
byte[] request = "{ id: \"test2\" }".getBytes(StandardCharsets.UTF_8);
int length = request.length;
http.setFixedLengthStreamingMode(length);
String userpass = username + ":" + password;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
http.setRequestProperty ("Authorization", basicAuth);
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
// http.connect();
try(OutputStream os = http.getOutputStream()) {
os.write(request);
os.flush();
InputStream in = http.getInputStream(); /// error !!!
System.out.println(in);
}
}
}