1414
1515import static com .sap .cloud .sdk .services .openapi .apache .apiclient .DefaultApiResponseHandler .isJsonMime ;
1616import static lombok .AccessLevel .PRIVATE ;
17+ import static org .apache .hc .core5 .http .HttpHeaders .CONTENT_ENCODING ;
1718
1819import java .io .ByteArrayOutputStream ;
1920import java .io .File ;
@@ -492,7 +493,7 @@ public <T> T invokeAPI(
492493 if ( body != null || !formParams .isEmpty () ) {
493494 if ( isBodyAllowed (Method .valueOf (method )) ) {
494495 // Add entity if we have content and a valid method
495- builder .setEntity (serialize (body , formParams , contentTypeObj , headerParams . get ( "Content-Encoding" ) ));
496+ builder .setEntity (serialize (body , formParams , contentTypeObj , headerParams ));
496497 } else {
497498 throw new OpenApiRequestException ("method " + method + " does not support a request body" );
498499 }
@@ -520,6 +521,8 @@ public <T> T invokeAPI(
520521 * Content type
521522 * @param formParams
522523 * Form parameters
524+ * @param headerParams
525+ * Header parameters, used to check content encoding for JSON serialization
523526 * @return Object
524527 * @throws OpenApiRequestException
525528 * API exception
@@ -529,12 +532,12 @@ private HttpEntity serialize(
529532 @ Nullable final Object body ,
530533 @ Nonnull final Map <String , Object > formParams ,
531534 @ Nonnull final ContentType contentType ,
532- @ Nullable final String contentEncoding )
535+ @ Nonnull final Map < String , String > headerParams )
533536 throws OpenApiRequestException
534537 {
535538 final String mimeType = contentType .getMimeType ();
536539 if ( isJsonMime (mimeType ) ) {
537- return serializeJson (body , contentType , contentEncoding );
540+ return serializeJson (body , contentType , headerParams );
538541 } else if ( mimeType .equals (ContentType .MULTIPART_FORM_DATA .getMimeType ()) ) {
539542 return serializeMultipart (formParams , contentType );
540543 } else if ( mimeType .equals (ContentType .APPLICATION_FORM_URLENCODED .getMimeType ()) ) {
@@ -551,18 +554,22 @@ private HttpEntity serialize(
551554 private HttpEntity serializeJson (
552555 @ Nullable final Object body ,
553556 @ Nonnull final ContentType contentType ,
554- @ Nullable final String contentEncoding )
557+ @ Nonnull final Map < String , String > headerParams )
555558 throws OpenApiRequestException
556559 {
557- if ( "gzip" .equals (contentEncoding ) ) {
560+ if ( "gzip" .equalsIgnoreCase (headerParams .get (CONTENT_ENCODING ))
561+ || "gzip" .equalsIgnoreCase (headerParams .get (CONTENT_ENCODING .toLowerCase ())) ) {
558562 val outputStream = new ByteArrayOutputStream ();
559563 try ( val gzip = new GZIPOutputStream (outputStream ) ) {
560564 gzip .write (objectMapper .writeValueAsBytes (body ));
561565 }
562566 catch ( IOException e ) {
563- throw new OpenApiRequestException (e );
567+ throw new OpenApiRequestException ("Failed to GZIP compress request body" , e );
564568 }
565- return new ByteArrayEntity (outputStream .toByteArray (), contentType .withCharset (StandardCharsets .UTF_8 ));
569+ return new ByteArrayEntity (
570+ outputStream .toByteArray (),
571+ contentType .withCharset (StandardCharsets .UTF_8 ),
572+ "gzip" );
566573 }
567574 try {
568575 return new StringEntity (
0 commit comments