Skip to content

Commit

Permalink
[notificationsforfiretv] Missing Content-Length (#521) (#569)
Browse files Browse the repository at this point in the history
* fixed: missing Content-Length header (BodyPublisher.ofByteArrays JDK 17 issue)

Signed-off-by: Tom Blum trinitus01@googlemail.com
(cherry picked from commit 5746302)
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K committed Mar 23, 2024
1 parent b9fc33c commit 448f17b
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
Expand Down Expand Up @@ -103,9 +104,15 @@ public void addFilePart(String name, File file) throws IOException {
public String send() throws IOException, InterruptedException {
byteArrays.add(("--" + boundary + "--").getBytes(StandardCharsets.UTF_8));

int length = 0;
for (byte[] bytes : byteArrays) {
length += bytes.length;
}

BodyPublisher bodyPublisher = BodyPublishers.fromPublisher(BodyPublishers.ofByteArrays(byteArrays), length);
HttpRequest httpRequest = HttpRequest.newBuilder()
.header("Content-Type", "multipart/form-data;boundary=" + boundary)
.POST(BodyPublishers.ofByteArrays(byteArrays)).uri(uri).build();
.header("Content-Type", "multipart/form-data;boundary=" + boundary).POST(bodyPublisher).uri(uri)
.build();
HttpResponse<String> response = httpClient.send(httpRequest, BodyHandlers.ofString());
if (response.statusCode() == HttpURLConnection.HTTP_OK) {
return response.body();
Expand Down

0 comments on commit 448f17b

Please sign in to comment.