Java HttpURLConnection OutputStreamWriter is empty

723 views Asked by At

I know this has been answered 10,000+ times. I have been reading and testing recommendations for over an hour without any progress.

In the code below, the request body is always empty.

URL url = new URL("[REMOVED]");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Authorization", "Bearer [REMOVED]");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("ABC");
writer.flush();
writer.close();

String line;
BufferedReader reader = new BufferedReader(new 
                             InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
  System.out.println(line);
}

reader.close();

The record is created on the server, but the body is always null.

Any help is appreciated.

1

There are 1 answers

0
alockrem On

The exact same code that does not work on localhost works on app engine. I don't understand why, but the code above works.