From e482f5d3d45517f3d56ba0cf0379731798090060 Mon Sep 17 00:00:00 2001 From: Syed Shahzeb Date: Mon, 16 Feb 2026 12:46:40 +0100 Subject: [PATCH] Handle gzip content encoding such that content stream can be read --- .../internal/http/response/HttpResponseUtils.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java b/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java index dd1e1fbf..6034b0dc 100644 --- a/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java +++ b/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java @@ -23,6 +23,7 @@ import no.digipost.api.client.representations.ErrorType; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpResponse; import org.apache.hc.core5.http.HttpStatus; @@ -32,6 +33,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.zip.GZIPInputStream; import static no.digipost.api.client.errorhandling.ErrorCode.GENERAL_ERROR; import static no.digipost.api.client.internal.ExceptionUtils.exceptionNameAndMessage; @@ -55,7 +57,14 @@ public static InputStream safelyOfferEntityStreamExternally(ClassicHttpResponse try { checkResponse(response, eventLogger); entity = response.getEntity(); - return entity.getContent(); + InputStream stream = entity.getContent(); + + Header encodingHeader = response.getFirstHeader("Content-Encoding"); + if (encodingHeader != null && "gzip".equalsIgnoreCase(encodingHeader.getValue())) { + return new GZIPInputStream(stream); + } + + return stream; } catch (IOException | RuntimeException e) { try (ClassicHttpResponse autoClosed = response) { EntityUtils.consume(entity);