diff --git a/core/src/main/scala/sttp/client4/requestBuilder.scala b/core/src/main/scala/sttp/client4/requestBuilder.scala index de46a42d3..30a99ec11 100644 --- a/core/src/main/scala/sttp/client4/requestBuilder.scala +++ b/core/src/main/scala/sttp/client4/requestBuilder.scala @@ -321,7 +321,8 @@ trait PartialRequestBuilder[+PR <: PartialRequestBuilder[PR, R], +R] * @see * [[sttp.model.Encodings]] */ - def compressBody(encoding: String): PR = withOptions(options.copy(compressRequestBody = Some(encoding))) + def compressBody(encoding: String): PR = + withOptions(options.copy(compressRequestBody = Some(encoding))).header(HeaderNames.ContentEncoding, encoding) /** Set the HTTP version with which this request should be sent. Supported only in a few backends. */ def httpVersion(version: HttpVersion): PR = withOptions(options.copy(httpVersion = Some(version))) diff --git a/core/src/test/scala/sttp/client4/RequestTests.scala b/core/src/test/scala/sttp/client4/RequestTests.scala index 2e3c61f29..27e3536b0 100644 --- a/core/src/test/scala/sttp/client4/RequestTests.scala +++ b/core/src/test/scala/sttp/client4/RequestTests.scala @@ -3,6 +3,7 @@ package sttp.client4 import sttp.model.{Header, HeaderNames, StatusCode} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers +import sttp.model.Encodings class RequestTests extends AnyFlatSpec with Matchers { @@ -108,4 +109,10 @@ class RequestTests extends AnyFlatSpec with Matchers { Header("Content-Length", "4") ) } + + "compressBody" should "add the content encoding header" in { + emptyRequest.compressBody(Encodings.Gzip).headers.toSet shouldBe Set( + Header(HeaderNames.ContentEncoding, Encodings.Gzip) + ) + } }