The legacy java http client provided an OutputStream
using URLConnection.getOutputStream
. Writing a json body for example worked like the following:
final URLConnection urlConnection = ...;
try (OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream())) {
new Gson().toJson(someJsonObject, writer);
}
What's the equivalent way of stream-writing a request body with the new java 11 http client?
The only alternative I found is to write the entire output into a String
or byte[]
and use BodyPublishers.ofString/ofByteArray
, however this seems rather inefficient to me for larger requests.